Export the table data to excel and print it.

Source: Internet
Author: User

 

The following code is from http://blog.csdn.net/hellogv/. for reference, see the source!

Add the following two sentences:

# Include "utilcls. H"
# Include "comobj. HPP"

The following is the official start:

Void _ fastcall tmainform: toexcel (tadoquery * TT, ansistring Str) {// TT is the table of the exported data, STR is the command (For details, refer to the IF statement at the bottom of the Code) # define PG olepropertyget # define PS olepropertyset # define FN olefunction # define PR oleprocedurevariant Excel; try {Excel = createoleobject ("Excel. application "); // start Excel} catch (...) {showmessage ("Excel cannot be started. Check whether Excel is installed! ");} Excel. PS ("visible", (variant) True); // make Excel visible after it is started. PG ("workbooks "). FN ("add", 1); // single worksheet for (INT I = 0; I <tt-> fieldcount; I ++) // Add the field name {excel. exec (propertyget ("cells") <1 <I + 1 ). exec (propertyset ("value") <tt-> fieldlist-> strings [I]);} For (Int J = 0; j <tt-> fieldcount; j ++) // sort by field {tt-> first (); For (INT I = 0; I <tt-> recordcount; I ++) // mine in Data Order {excel. exec (propertyget ("cells") <I + 2 <j + 1 ). exec (Property Set ("value") <tt-> fieldbyname (TT-> fieldlist-> strings [J])-> asstring); TT-> next ();}} if (STR = "Export") {}// if it is an export, it will do nothing if (STR = "print") // if it is an Excel print. olepropertyget ("activeworkbook "). olepropertyget ("activesheet "). olefunction ("printout"); If (STR = "Print browse") // if it is print browse excel. olepropertyget ("activeworkbook "). olepropertyget ("activesheet "). olefunction ("printpreview"); excel. ~ Variant ();}//---------------------------------------------------------------------------

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.