⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 controlexceldoc.cpp

📁 VC操作Excel的示范源码
💻 CPP
字号:
// ControlExcelDoc.cpp : implementation of the CControlExcelDoc class
//

#include "stdafx.h"
#include "ControlExcel.h"

#include "ControlExcelDoc.h"

#include "excel9.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CControlExcelDoc

IMPLEMENT_DYNCREATE(CControlExcelDoc, CDocument)

BEGIN_MESSAGE_MAP(CControlExcelDoc, CDocument)
	//{{AFX_MSG_MAP(CControlExcelDoc)
	ON_COMMAND(ID_CONTROL_EXCEL, OnControlExcel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CControlExcelDoc construction/destruction

CControlExcelDoc::CControlExcelDoc()
{
	// TODO: add one-time construction code here

}

CControlExcelDoc::~CControlExcelDoc()
{
}

BOOL CControlExcelDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CControlExcelDoc serialization

void CControlExcelDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CControlExcelDoc diagnostics

#ifdef _DEBUG
void CControlExcelDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CControlExcelDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CControlExcelDoc commands

void CControlExcelDoc::OnControlExcel() 
{
	// TODO: Add your command handler code here
	COleVariant vtOptional((long)DISP_E_PARAMNOTFOUND,
		VT_ERROR),vtTrue((short)TRUE),vtFalse((short)FALSE);
	_Application *ExcelApp = new _Application;

	ExcelApp->CreateDispatch("excel.application");

	//设置为显示
	ExcelApp->SetVisible(TRUE);
	//得到WorkBooks
	Workbooks  ExcelBook=ExcelApp->GetWorkbooks();
	Sheets     ExcelSheet=ExcelBook.Add(vtOptional);
	_Workbook  workBook;
	_Worksheet workSheet;
	Range    range;
	workBook.AttachDispatch(ExcelApp->GetApplication());
	ExcelSheet=workBook.GetSheets();

	////////////////////////////////////////////
	workSheet=ExcelSheet.GetItem(COleVariant((short)1));
	workSheet.Activate();


	Range myallrange=workSheet.GetRange(COleVariant("A1"),COleVariant("D1"));
	//myallrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myallrange.SetHorizontalAlignment(COleVariant((short)3));
	myallrange.Merge(COleVariant((short)1));
	myallrange.SetValue(COleVariant("个人信息表"));

	Range myrange=workSheet.GetRange(COleVariant("A2"),COleVariant("A2"));
	myrange.SetValue(COleVariant("姓名"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)15));

	myrange=workSheet.GetRange(COleVariant("B2"),COleVariant("B2"));
	myrange.SetValue(COleVariant("性别"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)15));

	myrange=workSheet.GetRange(COleVariant("C2"),COleVariant("C2"));
	myrange.SetValue(COleVariant("QQ"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)15));

	myrange=workSheet.GetRange(COleVariant("D2"),COleVariant("D2"));
	myrange.SetValue(COleVariant("Email"));
	myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
	myrange.SetHorizontalAlignment(COleVariant((short)3));
	myrange.SetVerticalAlignment(COleVariant((short)2));
	myrange.SetColumnWidth(COleVariant((short)17));

	CString str;
	for(int i=3;i<=5;i++)
	{
		str.Format("A%d",i);
		Range myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("相生昌"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));

		str.Format("B%d",i);
		myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("男"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));

		str.Format("C%d",i);
		myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("16432884"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));

		str.Format("D%d",i);
		myrange=workSheet.GetRange(COleVariant(str),COleVariant(str));
		myrange.SetValue(COleVariant("superxsc@126.com"));
		myrange.BorderAround(COleVariant((short)1),(long)2,(long)1,vtOptional);
		myrange.SetHorizontalAlignment(COleVariant((short)3));
		myrange.SetVerticalAlignment(COleVariant((short)2));
	}

	delete ExcelApp;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -