C++將DBGrid中數(shù)據(jù)導出到Word和Excel

字號:

經??吹接芯W友發(fā)帖子詢問如何將DBGrid中的內容導出到Excel或Word文檔中,于是筆者花了點時間寫了以下兩個函數(shù),分別實現(xiàn)將DBGrid中數(shù)據(jù)導出到Word和Excel文檔。需要注意的是DBGrid中的數(shù)據(jù)并不代表數(shù)據(jù)庫中所有的數(shù)據(jù),因為數(shù)據(jù)集在打開的時候有可能進行了篩選,取決于使用者如何打開這個數(shù)據(jù)集,總之就是DBGrid中顯示多少數(shù)據(jù),就導出多少。
    一、將DBGrid中的內容導出到Word文檔
    //---------------------------------------------------------------------------
    // 將DBGrid中的數(shù)據(jù)導出到Word文檔
    //---------------------------------------------------------------------------
    void __fastcall DBGrid2Word(TDBGrid *dbg, String strDocFile)
    {
    if(!dbg->DataSource->DataSet->Active) // 數(shù)據(jù)集沒有打開就返回
    return;
    Variant vWordApp, vTable, VCell;
    try
    {
    vWordApp = Variant::CreateObject("Word.Application");
    }
    catch(...)
    {
    MessageBox(0, "啟動 Word 出錯, 可能是沒有安裝Word.","DBGrid2Word", MB_OK | MB_ICONERROR);
    vWordApp = Unassigned;
    return;
    }
    // 隱藏Word界面
    vWordApp.OlePropertySet("Visible", false);
    // 新建一個文檔
    vWordApp.OlePropertyGet("Documents").OleFunction("Add");
    Variant vSelect = vWordApp.OlePropertyGet("Selection");
    // 設置一下字體,大小
    vSelect.OlePropertyGet("Font").OlePropertySet("Size", dbg->Font->Size);
    vSelect.OlePropertyGet("Font").OlePropertySet("Name", dbg->Font->Name.c_str());
    // 要插入表格的行數(shù)
    int nRowCount(dbg->DataSource->DataSet->RecordCount + 1);
    nRowCount = nRowCount < 2? 2: nRowCount;
    // 要插入表格的列數(shù)
    int nColCount(dbg->Columns->Count);
    nColCount = nColCount < 1? 1: nColCount;
    // 在Word文檔中插入與DBGrid行數(shù)列數(shù)基本相同的一個表格
    vWordApp.OlePropertyGet("ActiveDocument").OlePropertyGet("Tables").OleProcedure("Add",
    vSelect.OlePropertyGet("Range"),
    nRowCount, // 行數(shù)
    nColCount, // 列數(shù)
    1, // DefaultTableBehavior:=wdWord9TableBehavior
    0); // AutoFitBehavior:=wdAutoFitFixed
    // 操作這個表格
    vTable = vWordApp.OlePropertyGet("ActiveDocument").
    OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1);
    // 設置單元格的寬度
    for(int i=0; i    {
    int nColWidth = dbg->Columns->Items[i]->Width;
    vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1)
     .OlePropertySet("PreferredWidthType", 3); // wdPreferredWidthPoints
    vTable.OlePropertyGet("Columns").OleFunction("Item", i + 1)
     .OlePropertySet("PreferredWidth", nColWidth);
    }
    // 先將列名寫入Word表格
    for(int j=0; jColumns->Count; j++)
    {
    vCell = vTable.OleFunction("Cell", 1, j + 1);
    vCell.OlePropertySet("Range", dbg->Columns->Items[j]->FieldName.c_str());
    // 列名單元格背景顏色 // wdColorGray125
    vCell.OlePropertyGet("Shading").OlePropertySet("BackgroundPatternColor", 14737632);
    }
    // 將DBGrid中的數(shù)據(jù)寫入Word表格
    dbg->DataSource->DataSet->First();
    for(int i=0; i    {
    // 63 63 72 75 6E 2E 63 6F 6D
    for(int j=0; jColumns->Count; j++)
    {
    vCell = vTable.OleFunction("Cell", i + 2, j + 1);
    vCell.OlePropertySet("Range",
     dbg->DataSource->DataSet->FieldByName(
    dbg->Columns->Items[j]->FieldName)->AsString.c_str());
    }
    dbg->DataSource->DataSet->Next();
    }
    // 保存Word文檔并退出
    vWordApp.OlePropertyGet("ActiveDocument").OleProcedure("SaveAs", strDocFile.c_str());
    vWordApp.OlePropertyGet("ActiveDocument").OleProcedure("Close");
    Application->ProcessMessages();
    vWordApp.OleProcedure("Quit");
    Application->ProcessMessages();
    vWordApp = Unassigned;
    // 工作結束
    MessageBox(0, "DBGrid2Word 轉換結束!","DBGrid2Word", MB_OK | MB_ICONINFORMATION);
    }