在C++中將數(shù)據(jù)庫(kù)數(shù)據(jù)分行和列保存到Excel中

字號(hào):

如何在C++中將數(shù)據(jù)庫(kù)數(shù)據(jù)分行和列保存到Excel中? 程序中的數(shù)據(jù)在StringGrid控件中顯示的,那如何按照StringGrid顯示的格式分行分列保存到Excel表格呢?請(qǐng)看如下兩種方法的實(shí)現(xiàn):
    第一種方法:采用的一格一格填充數(shù)據(jù)  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;iRowCount;i++)
    {
    for(int j=0;jColCount;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;
    }
    第二種方法:直接從ADO把數(shù)據(jù)導(dǎo)出來(lái)  Variant ExcelApp;
    Variant WorkBook1;
    Variant Sheet1;
    Variant Range;
    Variant Table;
    Variant QueryTables;
    int Count;
    AnsiString ITemp,IStr;
    AnsiString sSQL,sSQLHj, sWhere, sStartDate, sEndDate, sDate;
    try
    {
    ExcelApp=CreateOleObject ("Excel.Application");
    }
    catch(...)
    {
    ShowMessage("運(yùn)行出錯(cuò),請(qǐng)確認(rèn)裝了Excel");
    return;
    }
    ExcelApp.OlePropertyGet("WorkBooks").OleFunction("Add");
    ExcelApp.OlePropertyGet("workbooks").OleFunction("Add", "E:\\Lxrb6.xls");
    WorkBook1=ExcelApp.OlePropertyGet("ActiveWorkBook");
    Sheet1 = WorkBook1.OlePropertyGet("ActiveSheet");
    WorkBook1.OlePropertyGet("Sheets", 1).OleProcedure("Select");
    Sheet1.OlePropertySet("name","小熱報(bào)6");
    Range=Sheet1.OlePropertyGet("Range","A9");
    QueryTables=Sheet1.OlePropertyGet("QueryTables");
    sSQL="select * from Table" //這里的數(shù)據(jù)很多,我隨便簡(jiǎn)化下!
    qryTmp->Active=false;
    qryTmp->SQL->Clear();
    qryTmp->SQL->Add(sSQL);
    qryTmp->Active=true;
    Count=qryTmp->RecordCount+12;
    IStr="A9:M"+IntToStr(Count);
    Table=QueryTables.OleFunction("Add",qryTmp->Recordset,Range);
    Table.OlePropertySet("FieldNames",false);
    Range=Sheet1.OlePropertyGet("Range", IStr.c_str());
    Sheet1.OlePropertyGet("Range", "A:K").OlePropertyGet("Columns").OleProcedure("AutoFit"); //自動(dòng)列寬
    Table.OleProcedure("Refresh",true);
    WorkBook1.OleFunction("SaveAs", "E:\\6.xls");
    ShowMessage("導(dǎo)出完畢,請(qǐng)檢查");
    qryTmp->Active=false;
    ExcelApp.Exec(Procedure("Quit"));
    ExcelApp = Unassigned;