由于学习对Excel的操作是临时学的,有很多东西只是一知半解,但好歹是能运行了,相关内容可以参照前两篇学习笔记。
本文中使用的是OLE方式读写,环境是VS2008和Office2003。现在的编译器有自动完成功能,很容易找到相应的成员函数。查看函数原型有助于理解参数的意义,但仍是有些参数也搞不明白是什么作用,或者如何设置。Excel相关的预定义枚举类型,都是Xl开头,比如XlHAlign,是水平对齐,可以用XlHAlign::找到居中、靠右、靠左等应该设置的值。
步骤如下:
1.新建MFC工程,Project - Add Class - MFC Class FromTypeLib,选择File,在Office安装目录下找到EXCEL.EXE,选择相应的接口(参考学习笔记二)。
2.VS2008会在工程中加入相应的头文件。打开头文件,把#import"D:\Program Files\Microsoft Office\Office12\EXCEL.EXE"no_namespace 一行改成:
#import "C:\Program Files\Common Files\MicrosoftShared\OFFICE11\MSO.DLL"
rename("RGB", "MSORGB")
rename("DocumentProperties","MSODocumentProperties")
rename("SearchPath","MsoSearchPath")
rename_namespace("Office")
using namespace Office;
#import "C:\Program Files\Common Files\MicrosoftShared\VBA\VBA6\VBE6EXT.OLB"
using namespace VBIDE;
#import "C:\Program Files\Microsoft Office\Office11\EXCEL.EXE"
rename("DialogBox", "ExcelDialogBox")
rename("RGB","ExcelRGB")
rename("CopyFile", "ExcelCopyFile")
rename("ReplaceText", "ExcelReplaceText")
no_auto_exclude
using namespace Excel;
#undef ExitWindows
#undef DialogBox
#undef RGB
注意“”字符后面不能放空格
3.由于许多Word的对象与Excel对象的名字是相同的,因此两者同时使用是,必须使用名字空间将其区分。方法简单,比如自定义名字空间是_excel,则把接口头文件中
class C***
{...};
改成
namespace _excel
{
class C***
{...};
};
4.完成,现在可以编程了。
下面是个简单的例子,包括合并单元格、设置单元格内容、设置单元格格式、设置边框、获取单元格内容、保存工作簿等功能
头文件部分
#include // 为了方便操作 VARIANT 类型变量,使用 CComVariant模板类
#include "CApplication.h"
#include "CBorders.h"
#include "CFont0.h"
#include "Cnterior.h"
#include "CRange.h"
#include "CWorkbook.h"
#include "CWorkbooks.h"
#include "CWorksheet.h"
#include "CWorksheets.h"
实现部分
void CMyExcelDlg::OnBnClickedOk()
{
_excel::CApplication app;
_excel::CBorders borders;
_excel::CFont0 font;
_excel::Cnterior interior;//背景色
_excel::CRange range;
_excel::CWorkbook workbook;
_excel::CWorkbooks workbooks;
_excel::CWorksheet worksheet;
_excel::CWorksheets worksheets;
CoInitialize(NULL);//初始化COM,最后还有CoUninitialize
COleVariantcovOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
if(!app.CreateDispatch(_T("EXCEL.application")))//启动WORD
{
AfxMessageBox(_T("居然你连OFFICE都没有安装吗?"));
return;
}
app.put_Visible(TRUE);//设置表可见性
app.put_DisplayFullScreen(FALSE);//设置全屏显示
app.put_DisplayAlerts(FALSE);//屏蔽警告
//打开工作簿
CString strPath;
strPath = "C:\Documents andSettings\Administrator\桌面\MyExcel\excel.xls";
workbooks = app.get_Workbooks();
try
{
workbook =workbooks.Add(_variant_t(strPath));
}
catch (CException* e)
{
workbook =workbooks.Add(vtMissing);//找不到就新建一个
}
//获取工作表worksheet(三种方法)
worksheet = workbook.get_ActiveSheet();
worksheets = workbook.get_Sheets();
//worksheet =worksheets.get_Item((_variant_t("sheet2")));
//worksheet =worksheets.get_Item((_variant_t(1)));
//新建工作表(此方法错,可能是参数问题)
worksheets.Add(vtMissing,vtMissing,_variant_t((long)1),_variant_t(XlSheetType::xlWorksheet));//第三个参数是新表的数量
//获取区域
range = worksheet.get_Range(_variant_t("A1"),_variant_t("A3"));
//合并单元格
range.Merge(_variant_t((long)0));
//设置单元格内容
range = worksheet.get_Range(_variant_t("B2"),_variant_t("C5"));
range.put_Value2(_variant_t("hey"));//参数是二维数组可以批量设置单元格值
//设置单元格格式
range = worksheet.get_Range(_variant_t("F2"),_variant_t("G5"));
range.put_HorizontalAlignment(_variant_t(XlHAlign::xlHAlignRight));//水平对齐
range.put_VerticalAlignment(_variant_t(XlVAlign::xlVAlignBottom));//竖直对齐
range.put_ColumnWidth(_variant_t(35));//单位不明
range.put_RowHeight(_variant_t(24));//磅
font = range.get_Font();//字体
font.put_Italic(_variant_t(1));//斜体
interior =range.get_Interior();//底色
interior.put_Color(_variant_t(76));
//获取单元格内容
CString getdata;//定义字符串变量
_variant_t rValue;
range =worksheet.get_Range(_variant_t("A1"),_variant_t("A1"));
rValue = range.get_Value2();
getdata = rValue.bstrVal;
//线框,border会对range中所有单元格都做设置,
range = worksheet.get_Range(_variant_t("A5"),_variant_t("C9"));
borders = range.get_Borders();
borders.put_LineStyle(_variant_t(XlLineStyle::xlDouble));
range = worksheet.get_Range(_variant_t("A12"),_variant_t("C13"));
range.BorderAround(_variant_t(XlLineStyle::xlDashDot),2, 8, vtMissing);//外框线
//另存为
covOptional =COleVariant((long)DISP_E_PARAMNOTFOUND,VT_ERROR);
COleVariantvarZero((short)0);
COleVariantvarTrue(short(1),VT_BOOL);
COleVariant varFalse(short(0),VT_BOOL);
workbook.SaveAs(COleVariant(strPath),COleVariant((long)XlFileFormat::xlWorkbookNormal), vtMissing,vtMissing,
vtMissing, vtMissing, (long)0,vtMissing,
vtMissing, vtMissing,vtMissing, vtMissing);
//workbook.Save();
AfxMessageBox(_T(""));
//所有东西都需要ReleaseDispatch,否则会报错
borders.ReleaseDispatch();
font.ReleaseDispatch();
interior.ReleaseDispatch();
range.ReleaseDispatch();
workbook.ReleaseDispatch();
workbooks.ReleaseDispatch();
worksheet.ReleaseDispatch();
worksheets.ReleaseDispatch();
app.Quit();//Excel退出
app.ReleaseDispatch();
CoUninitialize();//对应CoInitialize
OnOK();
}