VC calls the Execl template to generate a report

Source: Internet
Author: User

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 ();
}

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.