將DBGrid中的內容匯出到Excel文檔

來源:互聯網
上載者:User
//---------------------------------------------------------------------------
// 將DBGrid中的內容匯出到Word文檔
//---------------------------------------------------------------------------
void __fastcall DBGrid2Word(TDBGrid *dbg, String strDocFile)
{
    if(!dbg->DataSource->DataSet->Active) // 資料集沒有開啟就返回
        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());
    // 要插入表格的行數
    int nRowCount(dbg->DataSource->DataSet->RecordCount + 1);
    nRowCount = nRowCount < 2? 2: nRowCount;
    // 要插入表格的列數
    int nColCount(dbg->Columns->Count);
    nColCount = nColCount < 1? 1: nColCount;
    // 在Word文檔中插入與DBGrid行數列數基本相同的一個表格
    vWordApp.OlePropertyGet("ActiveDocument").OlePropertyGet("Tables")
        .OleProcedure("Add",
        vSelect.OlePropertyGet("Range"),
        nRowCount, // 行數
        nColCount, // 列數
        1, // DefaultTableBehavior:=wdWord9TableBehavior
        0); // AutoFitBehavior:=wdAutoFitFixed
    // 操作這個表格
    vTable = vWordApp.OlePropertyGet("ActiveDocument").
        OleFunction("Range").OlePropertyGet("Tables").OleFunction("Item", 1);
    // 設定儲存格的寬度
    for(int i=0; i<nColCount; 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; j<dbg->Columns->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中的資料寫入Word表格
    dbg->DataSource->DataSet->First();
    for(int i=0; i<nRowCount; i++)
    {
        // 63 63 72 75 6E 2E 63 6F 6D
        for(int j=0; j<dbg->Columns->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);
}
//---------------------------------------------------------------------------
// 將DBGrid中的內容匯出到Excel文檔
//---------------------------------------------------------------------------
void __fastcall DBGrid2Excel(TDBGrid *dbg, String strXlsFile)
{
    if(!dbg->DataSource->DataSet->Active) // 資料集沒有開啟就返回
        return;
    Variant vExcelApp, vSheet;
    try
    {
        vExcelApp = Variant::CreateObject("Excel.Application");
    }
    catch(...)
    {
        MessageBox(0, "啟動 Excel 出錯, 可能是沒有安裝Excel.",
                "DBGrid2Excel", MB_OK | MB_ICONERROR);
        return;
    }
    // 隱藏Excel介面
    vExcelApp.OlePropertySet("Visible", false);
    // 建立一個工作表
    vExcelApp.OlePropertyGet("Workbooks").OleFunction("Add", 1); // 工作表
    // 操作這個工作表
    vSheet = vExcelApp.OlePropertyGet("ActiveWorkbook")
            .OlePropertyGet("Sheets", 1);
    // 設定Excel文檔的字型
    vSheet.OleProcedure("Select");
    vSheet.OlePropertyGet("Cells").OleProcedure("Select");
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("Size", dbg->Font->Size);
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("Name", dbg->Font->Name.c_str());
    vExcelApp.OlePropertyGet("Selection").OlePropertyGet("Font")
            .OlePropertySet("FontStyle", "常規");
    vSheet.OlePropertyGet("Cells", 1, 1).OleProcedure("Select");
    // 表格的行數
    int nRowCount(dbg->DataSource->DataSet->RecordCount + 1);
    nRowCount = nRowCount < 2? 2: nRowCount;
    // 表格的列數
    int nColCount(dbg->Columns->Count);
    nColCount = nColCount < 1? 1: nColCount;
    // 設定儲存格的寬度
    for(int i=0; i<nColCount; i++)
    {
        int nColWidth = dbg->Columns->Items[i]->Width;
        vExcelApp.OlePropertyGet("Columns", i + 1)
                .OlePropertySet("ColumnWidth", nColWidth / 7);
    }
    // 先將列名寫入Excel表格
    for(int j=0; j<dbg->Columns->Count; j++)
    {
        // 標題列的行高
        vExcelApp.OlePropertyGet("Rows", 1).OlePropertySet("RowHeight", 20);
        //
        vSheet.OlePropertyGet("Cells", 1, j + 1)
                .OlePropertySet("Value",
                dbg->Columns->Items[j]->FieldName.c_str());
        // 設定列名儲存格的背景色
        Variant vInter = vSheet.OlePropertyGet(
                "Cells", 1, j + 1).OlePropertyGet("Interior");
        vInter.OlePropertySet("ColorIndex", 15); // 灰色
        vInter.OlePropertySet("Pattern", 1); // xlSolid
        vInter.OlePropertySet("PatternColorIndex", -4105); // xlAutomatic
    }
    // 將DBGrid中的資料寫入Excel表格
    dbg->DataSource->DataSet->First();
    for(int i=0; i<nRowCount; i++)
    {
        // 普通資料行的行高16
        vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16);
        // 63 63 72 75 6E 2E 63 6F 6D
        for(int j=0; j<dbg->Columns->Count; j++)
        {
            vSheet.OlePropertyGet("Cells", i + 2, j + 1)
                .OlePropertySet("Value",
                dbg->DataSource->DataSet->FieldByName(
                dbg->Columns->Items[j]->FieldName)->AsString.c_str());
        }
        dbg->DataSource->DataSet->Next();
    }
    // 儲存Excel文檔並退出
    vExcelApp.OlePropertyGet("ActiveWorkbook")
            .OleFunction("SaveAs", strXlsFile.c_str());
    vExcelApp.OleFunction("Quit");
    vSheet = Unassigned;
    vExcelApp = Unassigned;
    // 工作結束
    MessageBox(0, "DBGrid2Excel 轉換結束!",
            "DBGrid2Excel", MB_OK | MB_ICONINFORMATION);
}
//--------------------------------------------------------------------------- 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.