Excel as the output of the report or a more common way, so that you can take full advantage of Excel's powerful editing capabilities, as well as printing capabilities. The template approach is a good way to first make a template in Excel and then call the template when you generate the report. This makes it easy to generate the reports we need. This mainly describes how to load templates and add each page report. The code is as follows:
void Cexceltestdlg::onbutton1 ()
{
Todo:add your control notification handler code here
_application excelapp;
Workbooks Wbsmybooks;
_workbook Wbmybook;
Worksheets wssmysheets;
_worksheet Wsmysheet;
Range Rgmyrge;
Create Excel 2000 Server (start Excel)
if (! Excelapp.createdispatch ("Excel.Application", NULL)
{
AfxMessageBox ("Create Excel Service failed!");
Exit (1);
}
Excelapp.setvisible (FALSE);
Create a new document with a template file
Char Path[max_path];
GetCurrentDirectory (Max_path,path);
CString strpath = path;
strpath + = "\\template1";
Wbsmybooks.attachdispatch (Excelapp.getworkbooks (), true);
Wbmybook.attachdispatch (Wbsmybooks.add (_variant_t (strpath)));
Get worksheets
Wssmysheets.attachdispatch (Wbmybook.getworksheets (), true);
Get Sheet1
Wsmysheet.attachdispatch (Wssmysheets.getitem (_variant_t ("Sheet1")), true);
Add number of templates
for (int i=0;i<4;i++) {
Wsmysheet.copy (vtmissing,_variant_t (Wsmysheet));
}
CString str1;
Wsmysheet.attachdispatch (Wssmysheets.getitem (_variant_t ("Sheet1")), true);
str1 = "1th page";
Wsmysheet.setname (STR1);
For (I=0;i<wssmysheets.getcount () -1;i++) {
This place is missing two items when it is displayed
Wsmysheet = Wsmysheet.getnext ();
Str1. Format ("page%d", i+2);
Wsmysheet.setname (STR1);
}
Wsmysheet.attachdispatch (Wssmysheets.getitem (_variant_t ("1th page")), true);
Get all cells, at this time, Rgmyrge is a collection of cells
Rgmyrge.attachdispatch (Wsmysheet.getcells (), true);
Set the value of a cell in a 1 row 1 column
CString str=_text ("21:12");
Rgmyrge.setitem (_variant_t ((long) 7), _variant_t ((Long) 3), _variant_t (str));
Rgmyrge.setitem (_variant_t ((long) 9), _variant_t ((Long) 3), _variant_t (str));
Rgmyrge.setitem (_variant_t ((long) 7), _variant_t ((Long) 4), _variant_t (str));
Rgmyrge.setitem (_variant_t ((long) 8), _variant_t ((Long) 4), _variant_t (str));
Rgmyrge.setitem (_variant_t ((long) 9), _variant_t ((Long) 4), _variant_t (str));
Rgmyrge.setitem (_variant_t (Long), _variant_t ((Long) 4), _variant_t (str));
Rgmyrge.setitem (_variant_t (Long), _variant_t ((Long) 4), _variant_t (str));
Rgmyrge.setitem (_variant_t (Long), _variant_t ((Long) 4), _variant_t (str));
Save a table
strpath = path;
strpath + = "\\ylhexcel.xls";
Wsmysheet.saveas (Strpath,vtmissing,vtmissing,vtmissing,vtmissing,
vtmissing,vtmissing,vtmissing,vtmissing);
Excelapp.setvisible (TRUE);
Wbmybook.printpreview (_variant_t (false));
Releasing objects
Rgmyrge.releasedispatch ();
Wsmysheet.releasedispatch ();
Wssmysheets.releasedispatch ();
Wbmybook.releasedispatch ();
Wbsmybooks.releasedispatch ();
Excelapp.releasedispatch ();
}