猿问

求一个在VS2008中用MFC自动化EXCEL2007实例

我一直在学习MFC自动化EXCEL2007,可是很难进步。在这里想占用大侠5分钟时间,传一个成功的实例给我。将感激不尽。

环境是XP系统32位。VS2008。EXCEL2007
条件:
  1。把Sheet1中A1位上的数字用C++语言乘以2后写入Sheet2中A1位上。
  2. 把Sheet1中(C2,E5)位上的数字用C++语言乘以2后写入Sheet2中(C2,E5)位上。
  3. 必须运行成功。

慕工程0101907
浏览 476回答 1
1回答

侃侃无极

我以前写过一个应用,比较复杂,而且还是VC++ 6.0的,等明天我给你传个简单的。我新建了一个Win32 控制台程序,在 stdafx.h 的结尾处添加:&nbsp;#import "c:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE12\\MSO.DLL" rename_namespace("Office")using namespace Office;#import "c:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.olb" rename_namespace("VBE6")using namespace VBE6;#import "c:\\Program Files\\Microsoft Office\\OFFICE12\\EXCEL.EXE" rename("DialogBox", "DialogBoxXL") rename("RGB", "RBGXL") rename("DocumentProperties", "DocumentPropertiesXL") exclude ("IFont","IPicture"),rename_namespace("MSExcel")using namespace MSExcel;#import "c:\\Program Files\\Common Files\\Designer\\msaddndr.dll" raw_interfaces_only, raw_native_types, no_namespace, named_guids &nbsp;&nbsp;&nbsp;下面的为对Excel的操作代码:&nbsp;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ &nbsp; &nbsp;int nRetCode = 0; &nbsp; &nbsp;// 初始化 MFC 并在失败时显示错误 &nbsp; &nbsp;if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp;// TODO: 更改错误代码以符合您的需要 &nbsp; &nbsp; &nbsp; &nbsp;_tprintf(_T("错误: MFC 初始化失败\n")); &nbsp; &nbsp; &nbsp; &nbsp;nRetCode = 1; &nbsp; &nbsp;} &nbsp; &nbsp;else &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp;// TODO: 在此处为应用程序的行为编写代码。 &nbsp; &nbsp; &nbsp; &nbsp;HRESULT hr=NULL; &nbsp; &nbsp; &nbsp; &nbsp;// 初始化 COM 库 &nbsp; &nbsp; &nbsp; &nbsp;hr = CoInitialize(NULL); &nbsp; &nbsp; &nbsp; &nbsp;if(FAILED(hr)) &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_tprintf(_T("错误: COM 初始化失败\n")); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nRetCode = 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nRetCode; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;LPDISPATCH lpDisp=NULL; &nbsp; &nbsp; &nbsp; &nbsp;CLSID clsid; &nbsp; &nbsp; &nbsp; &nbsp;::COleDispatchDriver codd; &nbsp; &nbsp; &nbsp; &nbsp;COleException* pError=new COleException; &nbsp; &nbsp; &nbsp; &nbsp;// 获得EXCEL的CLSID &nbsp; &nbsp; &nbsp; &nbsp;::CLSIDFromProgID(L"Excel.Application",&clsid); &nbsp; &nbsp; &nbsp; &nbsp;// 打开 Excel Application 。 &nbsp; &nbsp; &nbsp; &nbsp;if(!codd.CreateDispatch(clsid,pError)) &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pError->Delete(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_tprintf(_T("错误: 打开 Excel Application 失败\n")); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nRetCode = 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nRetCode; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;CComQIPtr <MSExcel::_Application> pApp(codd.m_lpDispatch); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 显示 Excell Application。 &nbsp; &nbsp; &nbsp; &nbsp;pApp->PutVisible(0,TRUE); &nbsp; &nbsp; &nbsp; &nbsp;codd.ReleaseDispatch(); &nbsp; &nbsp; &nbsp; &nbsp;CComQIPtr <MSExcel::Workbooks> pCurDocs; &nbsp; &nbsp; &nbsp; &nbsp;CComQIPtr <MSExcel::_Workbook> pCurDoc; &nbsp; &nbsp; &nbsp; &nbsp;CComQIPtr <MSExcel::_Worksheet> pCurSheet; &nbsp; &nbsp; &nbsp; &nbsp;CComQIPtr <MSExcel::Range> pCurRange; &nbsp; &nbsp; &nbsp; &nbsp;COleVariant vTrue((short)TRUE),vFalse((short)FALSE); &nbsp; &nbsp; &nbsp; &nbsp;COleVariant vNull(L""); &nbsp; &nbsp; &nbsp; &nbsp;COleVariant vZero((short)0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COleVariant vOptional((long)DISP_E_PARAMNOTFOUND, &nbsp; VT_ERROR); &nbsp; &nbsp; &nbsp; &nbsp;// 保存文件名。 &nbsp; &nbsp; &nbsp; &nbsp;COleVariant vDocName(L"CppUseExcel.xlsx"); &nbsp; &nbsp; &nbsp; &nbsp;// 获取 Workbooks。 &nbsp; &nbsp; &nbsp; &nbsp;pCurDocs=pApp->GetWorkbooks(); &nbsp; &nbsp; &nbsp; &nbsp;// 新建空白工簿。 &nbsp; &nbsp; &nbsp; &nbsp;pCurDoc=pCurDocs->Add(vNull,0); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 获取 Sheet1 。 &nbsp; &nbsp; &nbsp; &nbsp;pCurSheet = pCurDoc->Sheets->GetItem(1); &nbsp; &nbsp; &nbsp; &nbsp;// 获取 Sheet1 的 A1 单元格,并填写初始值. &nbsp; &nbsp; &nbsp; &nbsp;pCurRange = pCurSheet->GetRange(L"A1"); &nbsp; &nbsp; &nbsp; &nbsp;int initValue = 1234; &nbsp; &nbsp; &nbsp; &nbsp;pCurRange->FormulaR1C1 = initValue; &nbsp; &nbsp; &nbsp; &nbsp;// 获取 Sheet1 的 A1 单元格的值. &nbsp; &nbsp; &nbsp; &nbsp;int nValueA1 = pCurRange->GetValue2(); &nbsp; &nbsp; &nbsp; &nbsp;// 获取 [C2,E5] 单元格,并填写初始值. &nbsp; &nbsp; &nbsp; &nbsp;pCurRange = pCurSheet->GetRange(L"C2",L"E5"); &nbsp; &nbsp; &nbsp; &nbsp;pCurRange->FormulaR1C1 = initValue; &nbsp; &nbsp; &nbsp; &nbsp;// 获取 Sheet1 的 [C2,E5] &nbsp;单元格的值. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_variant_t vC2E5= pCurRange->GetValue2(); &nbsp; &nbsp; &nbsp; &nbsp;// 获取 Sheet2 ,并将 Sheet1 A1 单元格的值乘以 2 后填入 Sheet2 的 A1 单元格. &nbsp; &nbsp; &nbsp; &nbsp;// &nbsp;Sheet1 &nbsp;[C2,E5] 单元格的值乘以 2 后填入 Sheet2 的 &nbsp;[C2,E5] 单元格. &nbsp; &nbsp; &nbsp; &nbsp;pCurSheet = pCurDoc->Sheets->GetItem(2); &nbsp; &nbsp; &nbsp; &nbsp;pCurRange = pCurSheet->GetRange(L"A1"); &nbsp; &nbsp; &nbsp; &nbsp;pCurRange->FormulaR1C1 = nValueA1 *2; &nbsp; &nbsp; &nbsp; &nbsp;pCurRange = pCurSheet->GetRange(L"C2",L"E5"); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 这里涉及到对 SAFEARRAY 中元素的操作,暂时不写,如果你确定是 &nbsp; &nbsp; &nbsp; &nbsp;// 在 Range [C2,E5] 中填写数组,我再写这个过程. &nbsp; &nbsp; &nbsp; &nbsp;pCurRange->FormulaR1C1 =vC2E5; &nbsp; &nbsp; &nbsp; &nbsp;hr = pCurDoc->SaveAs( &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vDocName, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xlOpenXMLWorkbook, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vOptional, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vOptional, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vFalse, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vFalse, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xlNoChange, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;xlLocalSessionChanges, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vOptional, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vOptional, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vOptional, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;vOptional); &nbsp; &nbsp; &nbsp; &nbsp;if(FAILED(hr)) &nbsp; &nbsp; &nbsp; &nbsp;{ &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;_tprintf(_T("错误: 保存 Excel 文档失败\n")); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pError->Delete(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pApp->Quit(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nRetCode = 1; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return nRetCode; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp; &nbsp; &nbsp;pError->Delete(); &nbsp; &nbsp; &nbsp; &nbsp;pApp->Quit(); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp; &nbsp;return nRetCode;}
随时随地看视频慕课网APP
我要回答