1.導入Excel類型庫
使用Visual C++的擴展指令#import導入Excel類型庫:
代碼如下:
#import "C:\\Program Files\\Common Files\\microsoft shared\\OFFICE14\\MSO.DLL" \
rename("RGB","MsoRGB") \
rename("SearchPath","MsoSearchPath")
#import "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA6\\VBE6EXT.OLB"
#import "C:\\Program Files\\Microsoft Office\\Office14\\EXCEL.EXE" \
rename( "DialogBox", "ExcelDialogBox" ) \
rename( "RGB", "ExcelRGB" ) \
rename( "CopyFile", "ExcelCopyFile" ) \
rename( "ReplaceText", "ExcelReplaceText" ) \
exclude( "IFont", "IPicture" ) no_dual_interfaces
#import指令會從指定的可執行文件,動態鏈接庫等COM組件中導出類型庫(type lib),在Debug和Release臨時目錄中生成對應的類型庫頭文件(type lib header file),以供C++程序使用。如以上三條指令在編譯後會生成excel.tlh, mso.lh和vbetext.olb三個頭文件,可以在Debug和Release目錄中找到。
2.訪問Excel暴露的COM對象
下面是一段比較完整的訪問Excel的實例代碼。首先用生成的數據填充單元格,然後用這些單元格的數據生成了一個圖表(Chart):
代碼如下:
try
{
Excel::_ApplicationPtr pExcelApp;
HRESULT hr = pExcelApp.CreateInstance(L"Excel.Application");
ATLASSERT(SUCCEEDED(hr));
pExcelApp->Visible = true; // make Excel's main window visible
Excel::_WorkbookPtr pWorkbook = pExcelApp->Workbooks->Open(lpszPathName); // open excel file
Excel::_WorksheetPtr pWorksheet = pWorkbook->ActiveSheet;
pWorksheet->Name = L"Chart Data";
Excel::RangePtr pRange = pWorksheet->Cells;
const int nplot = 100;
const double xlow = 0.0, xhigh = 20.0;
double h = (xhigh-xlow)/(double)nplot;
pRange->Item[1][1] = L"x"; // read/write cell's data
pRange->Item[1][2] = L"f(x)";
for (int i=0;i<nplot;++i)
{
double x = xlow+i*h;
pRange->Item[i+2][1] = x;
pRange->Item[i+2][2] = sin(x)*exp(-x);
}
Excel::RangePtr pBeginRange = pRange->Item[1][1];
Excel::RangePtr pEndRange = pRange->Item[nplot+1][2];
Excel::RangePtr pTotalRange =
pWorksheet->Range[(Excel::Range*)pBeginRange][(Excel::Range*)pEndRange];
Excel::_ChartPtr pChart = pExcelApp->ActiveWorkbook->Charts->Add();
// refer to :
// http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.chart.chartwizard(v=vs.80).aspx
pChart->ChartWizard(
(Excel::Range*)pTotalRange,
(long)Excel::xlXYScatter,
6L,
(long)Excel::xlColumns,
1L,1L,
true,
L"My Graph",
L"x",L"f(x)");
pChart->Name = L"My Data Plot";
pWorkbook->Close(VARIANT_TRUE); // save changes
pExcelApp->Quit();
}
catch (_com_error& error)
{
ATLASSERT(FALSE);
ATLTRACE2(error.ErrorMessage());
}
在這段代碼中,Excel::_ApplicationPtr , Excel::_WorkbookPtr 和 Excel::_WorksheetPtr 等均是Visual C++ 編譯器根據#import指令自動生成的智能指針,實際上就是C++模板類_com_ptr_t<T>的typedef,其定義可在excel.tlh等類型庫頭文件中找到。
另外,由於#import指令中沒有指定raw_interface_only修飾符,Visual C++對Excel的COM接口進行了適當的封裝,以簡化COM接口屬性和方法的調用,並且將HRESULT返回值都轉換成了C++異常,因此,上面的這段代碼不需要每一步都堅持HRESULT,而是改為捕獲C++異常。