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