How to save your database data branches and columns in Excel in C + +

Source: Internet
Author: User

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;

}

Related Article

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.