1. Import an Excel database
Use the extension command of Visual C ++ # import to import an Excel database:
Copy codeThe Code is as follows: # 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
# The import command exports the type Library (type lib) from the specified executable file, dynamic link library, and Other COM components ), generate the corresponding type Library header file in the Debug and Release temporary directories for the C ++ program. For example, after the preceding three commands are compiled, the excel. tlh, mso. lh, and vbetext. olb files are generated, which can be found in the Debug and Release directories.
2. Access the COM object exposed by Excel
The following is a complete example of accessing Excel. First, fill the cells with the generated data, and then use the data of these cells to generate a Chart ):Copy codeThe Code is as follows: 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:
// 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 ());
}
In this Code, Excel: _ ApplicationPtr, Excel: _ WorkbookPtr and Excel: _ WorksheetPtr are all smart pointers automatically generated by the Visual C ++ compiler according to the # import command, it is actually a typedef of the c ++ template class _ com_ptr_t <T>, which can be defined in excel. the header files of library types such as tlh are found.
In addition, because the # import command does not specify the raw_interface_only modifier, Visual C ++ encapsulates the COM interface of Excel to simplify the calling of COM interface attributes and methods, in addition, the returned values of HRESULT are converted to C ++ exceptions. Therefore, the above Code does not need to stick to HRESULT in every step, but instead captures C ++ exceptions.