How do I save my database data branches and columns in Excel in C + +? The data in the program is displayed in the Stringgrid control, how do you save it to an Excel table according to the format of the Stringgrid display? See the implementation of the following two methods:
The first method: the use of a grid to fill the data
Variant Excelapp,workbook1,worksheet1;
//---------------------------------------------------------------------------
__fastcall Tform1::tform1 (tcomponent* Owner)
: Tform (Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall Tform1::button1click (tobject *sender)
{
Ansistring FileName = Extractfiledir (application->exename) + "\\a.xls";
Try
{
Excelapp=variant::createobject ("Excel.Application");
}
catch (...)
{
ShowMessage ("sorry! Excel cannot be launched ");
Return
}
Excelapp.olepropertyset ("Visible", true);
Excelapp.olepropertyget ("Workbooks"). Oleprocedure ("Open", Filename.c_str ());
Workbook1=excelapp.olepropertyget ("ActiveWorkbook");
Worksheet1=workbook1.olepropertyget ("ActiveSheet");
for (int i=0;i<stringgrid1->rowcount;i++)
{
for (int j=0;j<stringgrid1->colcount;j++)
{
Worksheet1.olepropertyget ("Cells", I+1, J+1)
. Olepropertyset ("Value", Stringgrid1->cells[j][i].c_str ());
}
}
Excelapp.olepropertyget ("ActiveWorkbook")
. Olefunction ("SaveAs", Filename.c_str ());
Excelapp.olefunction ("Quit");
WorkSheet1 = unassigned;
WorkBook1 = unassigned;
Excelapp = unassigned;
}