使用MFC操作EXCEL文件Word文档下载推荐.docx
《使用MFC操作EXCEL文件Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《使用MFC操作EXCEL文件Word文档下载推荐.docx(16页珍藏版)》请在冰豆网上搜索。
/Excel应用程序接口Workbooksbooks;
/工作薄集合_Workbookbook;
/工作薄Worksheetssheets;
/工作表集合_Worksheetsheet;
/工作表Rangerange;
/Excel中针对单元格的操作都应先获取其对应的Range对象Fontfont;
Rangecols;
/*COleVariant类为VARIANT数据类型的包装,在自动化程序中,通常都使用VARIANT数据类型进行参数传递。
故下列程序中,函数参数都是通过COleVariant类来转换了的。
*/covOptional可选参数的VARIANT类型COleVariantcovOptional(long)DISP_E_PARAMNOTFOUND,VT_ERROR);
app.CreateDispatch(Excel.Application)this-MessageBox(无法创建Excel应用!
return;
/获取工作薄集合books=app.GetWorkbooks();
/添加一个工作薄book=books.Add(covOptional);
/获取工作表集合sheets=book.GetSheets();
/获取第一个工作表sheet=sheets.GetItem(COleVariant(short)1);
/选择工作表中A1:
A1单元格区域range=sheet.GetRange(COleVariant(A1),COleVariant(A1);
/设置A1=HELLOEXCEL!
range.SetValue(COleVariant(HELLOEXCEL!
/调整格式,设置粗体font=range.GetFont();
font.SetBold(COleVariant(short)TRUE);
/选择A2单元格,插入一个公式=RAND()*100000,并设置A2数字格式为货币形式range=sheet.GetRange(COleVariant(A2),COleVariant(A2);
range.SetFormula(COleVariant(=RAND()*100000);
range.SetNumberFormat(COleVariant($0.00);
/选择A:
A列,设置宽度为自动适应cols=range.GetEntireColumn();
cols.AutoFit();
/显示Excel表格,并设置状态为用户可控制app.SetVisible(TRUE);
app.SetUserControl(TRUE);
2.打开一个已有的excel表格实例cppviewplaincopyCStringstrPath;
strPath+=C:
template.xlt;
/模板的路径CFileFindfilefind;
filefind.FindFile(strPath)AfxMessageBox(没有找到模版文档,请其查找);
LPDISPATCHlpDisp;
/接口指针books=app.GetWorkbooks();
lpDisp=books.Open(m_filepath,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional,covOptional);
/与的不同,是个参数的,直接在后面加了两个covOptional成功了book.AttachDispatch(lpDisp);
3.保存一个excel文件实例cppviewplaincopy1.book.SetSaved(TRUE);
4.另存一个excel文件实例cppviewplaincopy1.book.SaveAs(COleVariant(m_filename),covOptional,2.covOptional,covOptional,3.covOptional,covOptional,(long)0,4.covOptional,covOptional,covOptional,5.covOptional,covOptional);
/与的不同,是个参数的,直接在后面加了两个covOptional成功了5.释放一个excel文件实例经试验证实,不释放第二次使用excel时会中断,放在类的析构里面有时调用不到,主动调用最保险。
(有没有AttachDispatch()过都要释放,否则报错)cppviewplaincopy1./释放对象(相当重要!
)2.Rang.ReleaseDispatch();
3.sheet.ReleaseDispatch();
4.sheets.ReleaseDispatch();
5.book.ReleaseDispatch();
6.books.ReleaseDispatch();
7./退出程序8.app.Quit();
9./m_ExlApp一定要释放,否则程序结束后还会有一个Excel进程驻留在内存中,而且程序重复运行的时候会出错10.app.ReleaseDispatch();
6.修改一个excel单元格cppviewplaincopy1.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
2.range.SetValue2(COleVariant(value);
7.取出一个excel单元格实现Variant数据类型转换为CString类,这个只是一个示例,转换较为简单。
cppviewplaincopy1.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
2.COleVariantrValue;
3.rValue=COleVariant(range.GetValue2();
4.rValue.ChangeType(VT_BSTR);
5.returnCString(rValue.bstrVal);
8.还有释放问题是最重要的问题:
首先变量必须全释放,无论当初是否绑定过;
其次,程序释放和程序关闭的顺序必须是cppviewplaincopy1.app.Quit();
2.app.ReleaseDispatch();
3.如果顺如颠倒如下:
4.app.ReleaseDispatch();
5.app.Quit();
6.出现的后果是程序关闭后,excel进程仍然运行,所以无法正常打开程序曾经打开excel表格。
附录(操作类源码):
cppviewplaincopy1.#include./Stdafx.h2.#includeOptExcel.h3.#includeexcel.h4.#includecomdef.h5.6.7._Applicationapp;
8.Workbooksbooks;
9._Workbookbook;
10.Worksheetssheets;
11._Worksheetsheet;
12.Rangerange;
13.Rangecell;
14.Fontfont;
15.16.COleVariantcovOptional(long)DISP_E_PARAMNOTFOUND,VT_ERROR);
17.18.19./20./Function:
COptExcel21./Description:
初始化函数,初始化中附加excel应用程序22./Call:
app.CreateDispatch(_T(Excel.Application)23./24.COptExcel:
COptExcel(void)25.26.if(:
CoInitialize(NULL)=E_INVALIDARG)27.28.AfxMessageBox(_T(初始化Com失败!
29.return;
30.31.32./验证office文件是否可以正确运行33.34.if(!
app.CreateDispatch(_T(Excel.Application)35.36.AfxMessageBox(_T(无法创建Excel应用!
37.return;
38.39./在程序执行文件路径名中,剪掉执行文件名,得到程序路径,追加模板文件名,得到模板完整路径40.41.42.43.44.45./46./Function:
COptExcel47./Description:
析构函数,释放对象,非常重要,不全部释放,占用内存,下48./一次使用此类时会中断49./Call:
ReleaseDispatch()50./51.COptExcel:
COptExcel(void)52.53.54.55.56.books.ReleaseDispatch();
57.book.ReleaseDispatch();
58.sheets.ReleaseDispatch();
59.sheet.ReleaseDispatch();
60.range.ReleaseDispatch();
61.font.ReleaseDispatch();
62.cell.ReleaseDispatch();
63.64.app.Quit();
65.66.app.ReleaseDispatch();
67.:
CoUninitialize();
68.69.70.71./72./Function:
OpenExcelBook73./Description:
打开表名为filename的文件,注意,文件路径非自动生成,以后74./考虑从下处理方法75./Call:
GetAppPath()76./Input:
CStringfilename文件名77./78.boolCOptExcel:
OpenExcelBook(CStringfilename)79.80.CFileFindfilefind;
81.if(!
filefind.FindFile(filename)82.83.AfxMessageBox(_T(文件不存在);
84.returnfalse;
85.86.LPDISPATCHlpDisp;
/接口指针87.books=app.GetWorkbooks();
88.lpDisp=books.Open(filename,89.covOptional,covOptional,covOptional,covOptional,90.covOptional,covOptional,covOptional,covOptional,91.covOptional,covOptional,covOptional,covOptional,92.covOptional,covOptional93.);
/与office2000的不同,是个参数的,直接在后面加了两个covOptional成功了94.book.AttachDispatch(lpDisp);
95.sheets=book.GetSheets();
96.sheet=sheets.GetItem(COleVariant(short)1);
/与的不同,是个参数的,直接在后面加了两个covOptional成功了97.returntrue;
98.99.voidCOptExcel:
NewExcelBook()100.101.books=app.GetWorkbooks();
102.book=books.Add(covOptional);
103.sheets=book.GetSheets();
104.sheet=sheets.GetItem(COleVariant(short)1);
/与的不同,是个参数的,直接在后面加了两个covOptional成功了105.106.107./108./Function:
OpenExcelApp109./Description:
打开应用程序(要注意以后如何识别用户要打开的是哪个文件)110./111.voidCOptExcel:
OpenExcelApp(void)112.113.app.SetVisible(TRUE);
114.app.SetUserControl(TRUE);
115.116.117./118./Function:
SaveExcel119./Description:
用于打开数据文件,续存数据后直接保存120./121.voidCOptExcel:
SaveExcel(void)122.123.book.SetSaved(TRUE);
124.125.126./127./Function:
SaveAsExcel128./Description:
保存excel文件129./130.voidCOptExcel:
SaveAsExcel(CStringfilename)131.132.book.SaveAs(COleVariant(filename),covOptional,133.covOptional,covOptional,134.covOptional,covOptional,(long)0,covOptional,covOptional,covOptional,135.covOptional,covOptional);
136.137.138.139./140./Function:
SetCellValue141./Description:
修改单元格内的值142./Call:
IndexToString()从(x,y)坐标形式转化为“A1”格式字符串143./Input:
introw单元格所在行144./intcol单元格所在列145./intAlign对齐方式默认为居中146./147.voidCOptExcel:
SetCellValue(introw,intcol,intAlign)148.149.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
150.range.SetValue2(COleVariant(value);
151.cell.AttachDispatch(range.GetItem(COleVariant(long
(1),COleVariant(long
(1).pdispVal);
152.cell.SetHorizontalAlignment(COleVariant(short)Align);
153.154.155./156./Function:
GetCellValue157./Description:
得到的单元格中的值158./Call:
IndexToString()从(x,y)坐标形式转化为“A1”格式字符串159./Input:
introw单元格所在行160./intcol单元格所在列161./Return:
CString单元格中的值162./163.CStringCOptExcel:
GetCellValue(introw,intcol)164.165.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
166.COleVariantrValue;
167.rValue=COleVariant(range.GetValue2();
168.rValue.ChangeType(VT_BSTR);
169.returnCString(rValue.bstrVal);
170.171./172./Function:
SetRowHeight173./Description:
设置行高174./Call:
IndexToString()从(x,y)坐标形式转化为“A1”格式字符串175./Input:
introw单元格所在行176./177.voidCOptExcel:
SetRowHeight(introw,CStringheight)178.179.intcol=1;
180.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
181.range.SetRowHeight(COleVariant(height);
182.183./184./Function:
SetColumnWidth185./Description:
设置列宽186./Call:
IndexToString()从(x,y)坐标形式转化为“A1”格式字符串187./Input:
intcol要设置列宽的列188./CString宽值189./190.voidCOptExcel:
SetColumnWidth(intcol,CStringwidth)191.192.introw=1;
193.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
194.range.SetColumnWidth(COleVariant(width);
195.196.197./198./Function:
SetRowHeight199./Description:
设置行高200./Call:
IndexToString()从(x,y)坐标形式转化为“A1”格式字符串201./Input:
introw单元格所在行202./203.CStringCOptExcel:
GetColumnWidth(intcol)204.205.introw=1;
206.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
207.VARIANTwidth=range.GetColumnWidth();
208.CStringstrwidth;
209.strwidth.Format(CString(LPCSTR)(_bstr_t)(_variant_t)width);
210.returnstrwidth;
211.212.213./214./Function:
GetRowHeight215./Description:
设置行高216./Call:
IndexToString()从(x,y)坐标形式转化为“A1”格式字符串217./Input:
introw要设置行高的行218./CString宽值219./220.CStringCOptExcel:
GetRowHeight(introw)221.222.intcol=1;
223.range=sheet.GetRange(COleVariant(IndexToString(row,col),COleVariant(IndexToString(row,col);
224.VARIANTheight=range.GetRowHeight();
225.CStringstrheight;
226.strheight.Format(CString(LPCSTR)(_bstr_t)(_variant_t)height);
227.returnstrheight;
228.229.230.231./232./Function:
IndexToString233./Description:
得到的单元格在EXCEL中的定位名称字符串234./Input:
introw单元格所在行235./intcol单元格所在列236./Return:
CString单元格在EXCEL中的定位名称字符串237./238.CStringCOptExcel:
IndexToString(introw,intcol)239.240.CStringstrResult;
241.if(col26)242.243.strResult.Format(_T(%c%c%d),A+(col-1)/26-1,A+(col-1)%26,row);
244.245.else246.247.strResult.Format(_T(%c%d)