您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 咨询培训 > MFC操作Excel
MFC操作Excel下面的操作基于Excel2003一.初始化操作1.导入类库点击查看-建立类向导-AddClass...\FromatypeLibrary...-C:\ProgramFiles\MicrosoftOffice\Office\EXCEL.EXE,接下来就可以看到导入的类excel.h,excel.cpp。2.初始化COM找到App的InitInstance()函数,在其中添加AfxOleInit()函数的调用,如:[cpp]viewplaincopy1.if(!AfxOleInit())2.3.{4.5.AfxMessageBox(注册COM出错!);6.7.returnFALSE;8.9.}二.我自己写的Excel操作类ExcelOperate.h[cpp]viewplaincopy1.#includeatlbase.h2.#includeexcel.h3.usingnamespacemyexcel;4.classCExcelOperate5.{6.private:7.myexcel::_Applicationm_ecApp;8.Workbooksm_ecBooks;9._Workbookm_ecBook;10.Worksheetsm_ecSheets;11._Worksheetm_ecSheet;12.myexcel::Rangem_ecRange;13.VARIANTret;//保存单元格的值14.public:15.CExcelOperate();16.virtual~CExcelOperate();17.public:18.//操作19.//**********************创建新EXCEL*******************************************20.BOOLCreateApp();21.BOOLCreateWorkbooks();//创建一个新的EXCEL工作簿集合22.BOOLCreateWorkbook();//创建一个新的EXCEL工作簿23.BOOLCreateWorksheets();//创建一个新的EXCEL工作表集合24.BOOLCreateWorksheet(shortindex);//创建一个新的EXCEL工作表25.BOOLCreateSheet(shortindex);26.BOOLCreate(shortindex=1);//创建新的EXCEL应用程序并创建一个新工作簿和工作表27.voidShowApp();//显示EXCEL文档28.voidHideApp();//隐藏EXCEL文档29.//**********************打开文档*********************************************30.BOOLOpenWorkbook(CStringfileName,shortindex=1);31.BOOLOpen(CStringfileName);//创建新的EXCEL应用程序并打开一个已经存在的文档。32.BOOLSetActiveWorkbook(shorti);//设置当前激活的文档。33.34.//**********************保存文档*********************************************35.BOOLSaveWorkbook();//Excel是以打开形式,保存。36.BOOLSaveWorkbookAs(CStringfileName);//Excel以创建形式,保存。37.BOOLCloseWorkbook();38.voidCloseApp();39.//**********************读信息********************************40.BOOLGetRangeAndValue(CStringbegin,CStringend);//得到begin到end的Range并将之间的值设置到ret中41.voidGetRowsAndCols(long&rows,long&cols);//得到ret的行,列数42.BOOLGetTheValue(introws,intcols,CString&dest);//返回第rows,cols列的值,注意只返回文本类型的,到dest中43.BOOLSetTextFormat(CString&beginS,CString&endS);//将beginS到endS设置为文本格式(数字的还要用下面的方法再转一次)44.BOOLSetRowToTextFormat(CString&beginS,CString&endS);//将beginS到endS(包括数字类型)设置为文本格式45.46.};ExcelOperate.cpp[cpp]viewplaincopyCExcelOperate::CExcelOperate(){}CExcelOperate::~CExcelOperate(){}BOOLCExcelOperate::CreateApp(){//if(FALSE==m_wdApp.CreateDispatch(Word.Application))COleExceptionpe;if(!m_ecApp.CreateDispatch(_T(Excel.Application),&pe)){AfxMessageBox(Application创建失败,请确保安装了word2000或以上版本!,MB_OK|MB_ICONWARNING);pe.ReportError();throw&pe;returnFALSE;}returnTRUE;}BOOLCExcelOperate::CreateWorkbooks()//创建一个新的EXCEL工作簿集合{if(FALSE==CreateApp()){returnFALSE;}m_ecBooks=m_ecApp.GetWorkbooks();if(!m_ecBooks.m_lpDispatch){AfxMessageBox(WorkBooks创建失败!,MB_OK|MB_ICONWARNING);returnFALSE;}returnTRUE;}BOOLCExcelOperate::CreateWorkbook()//创建一个新的EXCEL工作簿{if(!m_ecBooks.m_lpDispatch){AfxMessageBox(WorkBooks为空!,MB_OK|MB_ICONWARNING);returnFALSE;}COleVariantvOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);m_ecBook=m_ecBooks.Add(vOptional);if(!m_ecBook.m_lpDispatch){AfxMessageBox(WorkBook为空!,MB_OK|MB_ICONWARNING);returnFALSE;}/*//得到document变量m_wdDoc=m_wdApp.GetActiveDocument();if(!m_wdDoc.m_lpDispatch){AfxMessageBox(Document获取失败!,MB_OK|MB_ICONWARNING);returnFALSE;}//得到selection变量m_wdSel=m_wdApp.GetSelection();if(!m_wdSel.m_lpDispatch){AfxMessageBox(Select获取失败!,MB_OK|MB_ICONWARNING);returnFALSE;}//得到Range变量m_wdRange=m_wdDoc.Range(vOptional,vOptional);if(!m_wdRange.m_lpDispatch){AfxMessageBox(Range获取失败!,MB_OK|MB_ICONWARNING);returnFALSE;}*/returnTRUE;}BOOLCExcelOperate::CreateWorksheets()//创建一个新的EXCEL工作表集合{if(!m_ecBook.m_lpDispatch){AfxMessageBox(WorkBook为空!,MB_OK|MB_ICONWARNING);returnFALSE;}m_ecSheets=m_ecBook.GetSheets();if(!m_ecSheets.m_lpDispatch){AfxMessageBox(WorkSheets为空!,MB_OK|MB_ICONWARNING);returnFALSE;}returnTRUE;}BOOLCExcelOperate::CreateWorksheet(shortindex)//创建一个新的EXCEL工作表{if(!m_ecSheets.m_lpDispatch){AfxMessageBox(WorkSheets为空!,MB_OK|MB_ICONWARNING);returnFALSE;}m_ecSheet=m_ecSheets.GetItem(COleVariant(index));if(!m_ecSheet.m_lpDispatch){AfxMessageBox(WorkSheet为空!,MB_OK|MB_ICONWARNING);returnFALSE;}returnTRUE;}BOOLCExcelOperate::CreateSheet(shortindex){if(CreateWorksheets()==FALSE){returnFALSE;}if(CreateWorksheet(index)==FALSE){returnFALSE;}returnTRUE;}BOOLCExcelOperate::Create(shortindex)//创建新的EXCEL应用程序并创建一个新工作簿和工作表{if(CreateWorkbooks()==FALSE){returnFALSE;}if(CreateWorkbook()==FALSE){returnFALSE;}if(CreateSheet(index)==FALSE){returnFALSE;}returnTRUE;}voidCExcelOperate::ShowApp()//显示WORD文档{m_ecApp.SetVisible(TRUE);}voidCExcelOperate::HideApp()//隐藏word文档{m_ecApp.SetVisible(FALSE);}//**********************打开文档*********************************************BOOLCExcelOperate::OpenWorkbook(CStringfileName,shortindex){if(!m_ecBooks.m_lpDispatch){AfxMessageBox(WorkSheets为空!,MB_OK|MB_ICONWARNING);returnFALSE;}//COleVariantvFileName(_T(fileName));COleVariantVOptional((long)DISP_E_PARAMNOTFOUND,VT_ERROR);m_ecBook=m_ecBooks.Open(fileName,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional,VOptional);if(!m_ecBook.m_lpDispatch){AfxMessageBox(WorkSheet获取失败!,MB_OK|MB_I
本文标题:MFC操作Excel
链接地址:https://www.777doc.com/doc-4255937 .html