ex_cep_tion 2017-12-02 08:20 采纳率: 0%
浏览 878

关于c++builder中导出到office的问题

请问c++builder中如何将DBGrid中的数据导出到word,Excel中,我建的一个表,然后我从中输入数据,用DBGrid显示,然后需要将表导出到word,Excel中,是有什么控件可以实现,还是需要自己写一些方法来实现

  • 写回答

1条回答 默认 最新

  • vayjay 2017-12-02 08:42
    关注

    // 导出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 {
    int nColWidth = dbg->Columns->Items[i]->Width;

    vExcelApp.OlePropertyGet("Columns", i + 1)

    .OlePropertySet("ColumnWidth", nColWidth / 7);

    }

    //---------------------------------------------------------------------------

    // 先将列名写入Excel表格

    for(int j=0; jColumns->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 {
    // 普通数据行的行高16
    vExcelApp.OlePropertyGet("Rows", i + 2).OlePropertySet("RowHeight", 16);
    // 63 63 72 75 6E 2E 63 6F 6D
    for(int j=0; jColumns->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);

    }

    评论

报告相同问题?

悬赏问题

  • ¥15 聚类分析或者python进行数据分析
  • ¥15 如何用visual studio code实现html页面
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?