将DBGrid中的数据导出到Word

转载:点击打开链接

这里把代码复制过来有一些细节上的要注意一下,在代码里面的for循环的次数和你的DBGrid绑定数据有很大关系,比如你是否隐藏某一行,或者说你只需导出哪几列等等;还有导出来的列名一般是DBGrid的列名,不是字段名称,所以注意这里;下面的代码是我根据我的要求改过的;希望对需要的人有用!!


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=1; i<nColCount; i++)
    {
        int nColWidth = dbg->Columns->Items[i]->Width;
        vTable.OlePropertyGet("Columns").OleFunction("Item", i )
                .OlePropertySet("PreferredWidthType", 3); // wdPreferredWidthPoints

        vTable.OlePropertyGet("Columns").OleFunction("Item", i)
                .OlePropertySet("PreferredWidth", nColWidth);
    }

    // 先将列名写入Word表格
    for(int j=1; j<dbg->Columns->Count; j++)
    {
        vCell = vTable.OleFunction("Cell", 1, j + 1);
        vCell.OlePropertySet("Range", dbg->Columns->Items[j]->Title->Caption.c_str());//这里是列名,不是字段名
        // 列名单元格背景颜色 // wdColorGray125
        vCell.OlePropertyGet("Shading")
                .OlePropertySet("BackgroundPatternColor", 14737632);
    }
    // 将DBGrid中的数据写入Word表格
    dbg->DataSource->DataSet->First();
    for(int i=1; i<nRowCount; i++)
    {
        // 63 63 72 75 6E 2E 63 6F 6D
        for(int j=1; 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);

}

void __fastcall TUserInfoForm::btnWordClick(TObject *Sender)
{
    if(MessageDlg("确认要导出数据吗?",
                mtConfirmation, TMsgDlgButtons() << mbYes << mbNo, 0)
                ==mrYes)
       {
                if(SaveDialog1->Execute())
                {
                       String fileName=SaveDialog1->FileName;
                       DBGrid2Word(DBGridUser,fileName);

               }
       }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值