C + + read and write Excel implementation method detailed _c language

Source: Internet
Author: User
1. Import Excel type library
To import an Excel type library using the extended instruction #import of Visual C + +:
Copy Code code 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

#import指令会从指定的可执行文件, the type library (type Lib) is exported in COM components such as dynamic link libraries, and the corresponding type library header file (type lib header files) is generated in the debug and release temp directory for use by C + + programs. If the above three instructions are compiled, EXCEL.TLH, MSO.LH and Vbetext.olb three header files will be generated, which can be found in the debug and release directories.
2. Access to exposed COM objects in Excel
The following is a relatively complete instance code to access Excel. First populate the cells with the generated data, and then use the data from those cells to generate a chart (Chart):
Copy Code code 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-&GT;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 ());
}

in this code, EXCEL::_APPLICATIONPTR, Excel::_workbookptr, and excel::_worksheetptr are the smart pointers that are automatically generated by the Visual C + + compiler based on the #import directives. is actually a _com_ptr_t<t> of the C + + template class, and its definition can be found in type library header files such as EXCEL.TLH.
Also, because the raw_interface_only modifier is not specified in the #import directive, Visual C + + encapsulates the COM interface for Excel to simplify calls to COM interface properties and methods and convert the HRESULT return value to C + + Exception, the above code does not need to persist the HRESULT every step, but instead captures the C + + exception.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.