The C++ implementation exports the table data to EXCEL and prints the instance code

  • 2020-05-17 06:06:21
  • OfStack

The implementation exports the table data to EXCEL and prints the instance code

First, add these two sentences:


#include "utilcls.h" #include "comobj.hpp"

Here we go:


void __fastcall TMainForm::ToExcel(TADOQuery *TT,AnsiString str) 
 
{//TT Is the table of exported data ,str For the command ( Look at the bottom of the code if statements ) 
 
#define PG OlePropertyGet 
 
#define PS OlePropertySet 
 
#define FN OleFunction 
 
#define PR OleProcedure 
 
 
 
Variant excel; 
 
try 
 
{ 
 
excel=CreateOleObject("Excel.Application"); // Start the Excel 
 
} 
 
catch(...) 
 
{ 
 
ShowMessage(" Unable to start Excel, Please check if it is installed EXCEL!"); 
 
} 
 
excel.PS("Visible", (Variant)true); // make Excel Visible after startup  
 
excel.PG("workbooks").FN("Add", 1); // Single worksheet  
 
for(int i=0;i<TT->FieldCount;i++) // First of all to EXCEL Add the field name of the table  
 
{ 
 
 excel.Exec(PropertyGet("Cells")<<1<<i+1).Exec(PropertySet("Value")<<TT->FieldList->Strings[i]); 
 
} 
 
 
 
for(int j=0;j<TT->FieldCount;j++)// Arrange 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(PropertySet("Value")<<TT->FieldByName(TT->FieldList->Strings[j])->AsString); 
 
  TT->Next(); 
 
 } 
 
 
 
} 
 
if(str==" export "){}// If you export, you do nothing, right  
 
if(str==" print ")// If it's printing  
 
 excel.OlePropertyGet("ActiveWorkBook").OlePropertyGet("ActiveSheet").OleFunction("PrintOut"); 
 
if(str==" Print to browse ")// If it's print browsing  
 
 excel.OlePropertyGet("ActiveWorkBook").OlePropertyGet("ActiveSheet").OleFunction("PrintPreview"); 
 
excel.~Variant(); 
 
 
 
} 

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: