将DBGrid的数据导出到word和excel中

DBGrid2Word和DBGrid2Excel,分别实现将DBGrid中数据导出到Word和Excel文档。需要注意的是DBGrid中的数据并不代码数据库中所有的数据,因为数据集在打开的时候有可能进行了筛选,取决于使用者如何打开这个数据集,总之就是DBGrid中显示多少数据,就导出多少
//---------------------------------------------------------------------------
// 将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 ->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);
// 本文转自 C++Builder研究 - http://www.ccrun.com/article.asp?i=635&d=g75jbn
}
//---------------------------------------------------------------------------
// 将DBGrid中的数据导出到Excel文档
// v0.1 by ccrun(老妖) 2005.10.13 1:55
//---------------------------------------------------------------------------
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->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);
}

// 测试代码
DBGrid2Word(DBGrid1, "C:\\ccrun\\123.doc");
DBGrid2Excel(DBGrid1, "C:\\ccrun\\123.xls");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
发布原因:在网上找了很久也没找到个好用的,不是慢,就是要依赖Excel(必须按装),又找呀找,才找到一个http://blog.csdn.net/xiangding/archive/2003/10/27/16918.aspx,但试了一下,不能用,不知是我用法不对还是本身有问题(由于没有给出使用示例),后来找到http://developer.51cto.com/art/200510/7494.htm这个不错,但他又让我装ehlid,不爽,所以对其做了修改,其它也就是去了个加页脚的地方,很爽,有进度条,导完后如果按安装了Excel则直接打开,如果没有的话就不管了,自已想办法打开吧!特点:一、不需安装Excel即可导出。二、有进度条。三、最多可以导出多少条,我没试,我试了5万条没有问题,时间也就是只需1-5分钟(我电脑较慢)。四、不需安装组件。使用方法(注:别忘了uses DBGridToExcel;):procedure TForm1.btn1Click(Sender: TObject);var DBGridToExcel: TDBGridToExcel; ExcelFileName: string;begin DBGridToExcel := TDBGridToExcel.Create(nil); try DBGridToExcel.TitleName := '入网车辆统计报表'; DBGridToExcel.BeginDate := '开始日期:2005-07-01'; DBGridToExcel.EndDate := '结束日期:2005-07-18'; DBGridToExcel.UserName := '系统管理员'; DBGridToExcel.DBGrid := dbgrd1; DBGridToExcel.ShowProgress := True; if ExcelFileName = '' then begin ExcelFileName := 'c:\1.xls'; with TSaveDialog.Create(nil) do begin Filter := 'Microsoft Excel xls文件|*.xls'; DefaultExt := 'xls'; if not Execute then Exit else ExcelFileName := FileName; end; end; DBGridToExcel.FileName := ExcelFileName; DBGridToExcel.Open := False; DBGridToExcel.ExportToExcel; finally DBGridToExcel.Free; end;end;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值