C++ Builder 将数据库数据导出到Excel中总结

首先要引入头文件:#include "ComObj.hpp"

下面是具体操作Excel:

 

bool __fastcall TFrmDispatchDataExport::DisExportData(String _strSql, String ExcelFileName)
{
 Variant ExcelApp,Workbook1,Sheet1;
 try
 {
  //建立Excel的Ole对象
  ExcelApp = CreateOleObject("Excel.Application");
  //设置Excel为不可见
//  ExcelApp.OlePropertySet("Visible",false);
  //创建Excel对象
//  ExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Add");
  ExcelApp.OlePropertyGet("WorkBooks").OleProcedure("Open", (WideString)ExcelFileName);
  //创建工作簿对象
  Workbook1 = ExcelApp.OlePropertyGet("ActiveWorkBook");
  //选择工作表中第一个工作表
//        Workbook1.OlePropertyGet("Sheets",1).OleProcedure("Select");
  //取当前活动Sheet
  Sheet1 = Workbook1.OlePropertyGet("ActiveSheet");

  //设置第一列的列宽为
  ExcelApp.OlePropertyGet("Columns",1).OlePropertySet("ColumnWidth",22);
  ExcelApp.OlePropertyGet("Columns",2).OlePropertySet("ColumnWidth",22);
  ExcelApp.OlePropertyGet("Columns",3).OlePropertySet("ColumnWidth",22);
  ExcelApp.OlePropertyGet("Columns",4).OlePropertySet("ColumnWidth",40);
  ExcelApp.OlePropertyGet("Columns",5).OlePropertySet("ColumnWidth",22);
  ExcelApp.OlePropertyGet("Columns",6).OlePropertySet("ColumnWidth",22);
  ExcelApp.OlePropertyGet("Columns",7).OlePropertySet("ColumnWidth",22);
  ExcelApp.OlePropertyGet("Columns",8).OlePropertySet("ColumnWidth",50);

  int iRows = 1;
        //使用C++ XE6版本 值要用WideString进行转换,否则会出错
  Sheet1.OlePropertyGet("Cells",iRows,1).OlePropertySet("Value",(WideString)"BUSIBOOKNO");
  Sheet1.OlePropertyGet("Cells",iRows,2).OlePropertySet("Value",(WideString)"附加码");
  Sheet1.OlePropertyGet("Cells",iRows,3).OlePropertySet("Value",(WideString)"付款企业代码");
  Sheet1.OlePropertyGet("Cells",iRows,4).OlePropertySet("Value",(WideString)"付款企业名称");
  Sheet1.OlePropertyGet("Cells",iRows,5).OlePropertySet("Value",(WideString)"OARBU");
  Sheet1.OlePropertyGet("Cells",iRows,6).OlePropertySet("Value",(WideString)"报价单号");
  Sheet1.OlePropertyGet("Cells",iRows,7).OlePropertySet("Value",(WideString)"付款类型");
  Sheet1.OlePropertyGet("Cells",iRows,8).OlePropertySet("Value",(WideString)"说明");

  qryExport->Close();
  qryExport->SQL->Clear();
  qryExport->SQL->Add(_strSql);
  qryExport->Open();
  qryExport->First();

  int nPos = 0;
  int nStep  = 0;
  int nRecordCnt = qryExport->RecordCount;
  if ( nRecordCnt > 0 )
  {
   nStep = ceil(nRecordCnt/100.0);
   pbExport->StepBy(1);
  }

  while( !qryExport->Eof )
  {
   ++iRows;
   //如果点击取消则终止线程
   if( bIsStop ){break;}
   for(int iCols = 0;iCols < qryExport->FieldCount;iCols++)
   {
    //如果点击取消则终止线程
    if( bIsStop ){break;}
    String strValue = qryExport->Fields->Fields[iCols]->AsString.Trim();
    Sheet1.OlePropertyGet("Cells",iRows,iCols+1).OlePropertySet("Value",(WideString)strValue);
   }
   ++nPos;

   //界面进度条操作
   if ( nPos%nStep == 0 )
   {
    pbExport->StepIt();
    Application->ProcessMessages();
   }
   qryExport->Next();
  }
 }
 __finally
 {
  //保存文件
  Workbook1.OleProcedure("Save");
  //关闭相关对象
  Workbook1.OleProcedure("Close");
  //退出Excel
//  ExcelApp.OlePropertySet("Quit",true);
  Sheet1=NULL;
  Workbook1=NULL;
  ExcelApp=NULL;
  ExcelApp=Unassigned;
  Workbook1=Unassigned;
  Sheet1=Unassigned;
  bFinsh = true;
  qryExport->Close();
 }

 return bFinsh;
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C Builder 是一个用于开发 Windows 应用程序的集成开发环境(IDE),它支持使用 Object Pascal 或 C++ 进行编程。C Builder 提供了多种方式来将数据库数据导入到应用程序。 首先,C Builder 提供了对多种数据库管理系统(DBMS)的支持,例如 MySQL、Oracle、SQL Server 等。我们可以通过使用 C Builder 提供的数据组件来连接数据库并执行查询。可以使用 TADOConnection 或 TIBDatabase 等组件来建立与数据库的连接。然后使用 TADOQuery 或 TIBQuery 等组件进行查询操作,获取数据库数据。 其次,C Builder 提供了数据集组件,例如 TDataSet 和 TClientDataSet。可以使用这些组件来在应用程序创建虚拟的数据集,然后利用数据连接组件将数据库数据填充到数据。通过设置 SQL 查询语句或使用视图来筛选需要的数据。然后可以在应用程序直接对数据集进行操作和展示。 另外,C Builder 还支持使用第三方数据库访问技术,例如 ADO(ActiveX Data Objects) 和 dbExpress。可以通过使用这些技术来连接和操作数据库数据总结起来,C Builder 提供了方便易用的数据连接、查询和操作功能,可以通过使用数据组件、数据集组件或第三方技术来导入数据库数据。这些功能使得开发人员能够轻松地将数据库数据集成到应用程序,实现数据的查询、展示和处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值