c++builder编数据库中的记录导出到excel.docx

上传人:b****7 文档编号:10214260 上传时间:2023-02-09 格式:DOCX 页数:12 大小:20.85KB
下载 相关 举报
c++builder编数据库中的记录导出到excel.docx_第1页
第1页 / 共12页
c++builder编数据库中的记录导出到excel.docx_第2页
第2页 / 共12页
c++builder编数据库中的记录导出到excel.docx_第3页
第3页 / 共12页
c++builder编数据库中的记录导出到excel.docx_第4页
第4页 / 共12页
c++builder编数据库中的记录导出到excel.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

c++builder编数据库中的记录导出到excel.docx

《c++builder编数据库中的记录导出到excel.docx》由会员分享,可在线阅读,更多相关《c++builder编数据库中的记录导出到excel.docx(12页珍藏版)》请在冰豆网上搜索。

c++builder编数据库中的记录导出到excel.docx

c++builder编数据库中的记录导出到excel

我正在用c++builder编写一个程序,想将数据库中的记录导出到excel中。

请问用c++builder中的什么控件?

代码如何写?

首先把Excel报表文件保存到一个指定目录下,最好放在可执行程序的子目录下,作为模板文件。

可以把报表标题、表头等设置好。

这里是保存在trpt子目录下。

  然后建一个report目录,作为报表目标文件夹,存放填好数据的报表,可以由用户直接操作。

  首先确定在你的机器中装有Office。

这里一Office2000为例。

  在C++Builder中新建一个工程,在窗体Form1上面放一个两个按钮SaveButton和ReadButton,分别用来保存数据到Excel表和显示刚刚保存的Excel表。

  在SaveButton按钮的单击事件中把从数据库中取到的数据放入到指定的Excel表中并将改文件拷贝到report目录下。

在ReadButto按钮的单击事件中显示report目录下的报表文件,方便用户修改和另外保存。

  在Form1.h头文件中定义几个变量:

private:

VariantEx,Wb,Sheet,ERange,EBorders;  

  并在文件头中包含如下语句:

#include"Excel_2K_SRVR.h"

#include  

  在Form1.cpp的文件头中加入

#pragmalink"Excel_2K_SRVR"  

  主要代码如下:

void__fastcallTForm1:

:

SaveButtonClick(TObject*Sender)

{

try

{

SaveButton->Enabled=false;

ReadButton->Enabled=false;//使两个按钮无效

file:

//取报表文件CardSend.xls的完整目录名

AnsiStringExcelFileName=GetCurrentDir()+"\\trpt\\table.xls";  

if(!

FileExists(ExcelFileName))

{

Application->MessageBox("报表模板文件不存在,无法打开!

",

"错误",MB_ICONSTOP|MB_OK);

return;

}

file:

//建立Excel的Ole对象Ex

try

{

Ex=Variant:

:

CreateObject("Excel.Application");

}

catch(...)

{

Application->MessageBox("无法启动Excel","错误",MB_ICONSTOP|MB_OK);

return;

}

file:

//设置Excel为不可见

Ex.OlePropertySet("Visible",false);

file:

//打开指定的Excel报表文件。

报表文件中最好设定只有一个Sheet。

Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",ExcelFileName.c_str());

Wb=Ex.OlePropertyGet("ActiveWorkBook");

Sheet=Wb.OlePropertyGet("ActiveSheet");//获得当前默认的Sheet

file:

//清空Excel表,这里是用循环清空到第300行。

对于一般的表格已经足够了。

AnsiStringstrRowTemp;  

AnsiStringstrRange;

intiCols,iRows;//记录列数和行数

/*从第三行开始,到第300行止。

一般第一行是表标题,第二行是副标题或者制表日期。

*/

for(iRows=3;iRows<300;iRows++)

{file:

//假设只有6列。

for(iCols=1;iCols<7;iCols++)

{

file:

//清空行

Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value","");

}

file:

//去掉表格边框

strRange="A"+IntToStr(iRows)+":

F"+IntToStr(iRows);//获取操作范围

ERange=Sheet.OlePropertyGet("Range",strRange.c_str());

EBorders=ERange.OlePropertyGet("Borders");//获取边框对象

EBorders.OlePropertySet("linestyle",xlNone);

}

AnsiStringstrPtrDate;file:

//存放当前日期,作为制表日期

DateSeparator='-';

ShortDateFormat="yyyy/m/d";//设置为年/月/日格式

strPtrDate=DateToStr(Date());//取当前日期

AnsiStringstrYear=strPtrDate.SubString(1,4);

strPtrDate=strPtrDate.SubString(6,strPtrDate.Length()-5);

AnsiStringstrMonth=strPtrDate.SubString(1,strPtrDate.Pos("-")-1);

AnsiStringstrDay=  

strPtrDate.SubString(strPtrDate.Pos("-")+1,

strPtrDate.Length()-strPtrDate.Pos("-"));

strPtrDate=strYear+"年"+strMonth+"月"+strDay+"日";

AnsiStringstrData="报表标题";//报表标题

file:

//将报表标题置于第一行第一列。

在此之前,应将报表文件的标题格式设定好。

Sheet.OlePropertyGet("Cells",1,1).OlePropertySet("Value",

strData.c_str());

file:

//将制表日期置于表格第二行的右侧。

Sheet.OlePropertyGet("Cells",2,5).OlePropertySet("Value",

strPtrDate.c_str());

iRows=3;//在第三行放置表格的列名

Sheet.OlePropertyGet("Cells",iRows,1).OlePropertySet("Value","列名1");

Sheet.OlePropertyGet("Cells",iRows,2).OlePropertySet("Value","列名2");

Sheet.OlePropertyGet("Cells",iRows,3).OlePropertySet("Value","列名3");

Sheet.OlePropertyGet("Cells",iRows,4).OlePropertySet("Value","列名4");

Sheet.OlePropertyGet("Cells",iRows,5).OlePropertySet("Value","列名5");

Sheet.OlePropertyGet("Cells",iRows,6).OlePropertySet("Value","列名6");

file:

//画表格边框,在A3:

F3之间取范围

strRange="A"+IntToStr(iRows)+":

F"+IntToStr(iRows);

ERange=Sheet.OlePropertyGet("Range",strRange.c_str());

EBorders=ERange.OlePropertyGet("Borders");

EBorders.OlePropertySet("linestyle",xlContinuous);

EBorders.OlePropertySet("weight",xlThin);

EBorders.OlePropertySet("colorindex",xlAutomatic);

iRows++;

file:

//从数据库中取数据(略),假设数据集放入Query1中。

Query1->Open();//打开数据集

file:

//循环取数

while(!

Query1->Eof)

{  

file:

//循环取字段的数据放到Excel表对应的行列中

for(iCols=1;iCols<7;iCols++)

{  

strRowTemp=Query1->Fields->Fields[iCols-1]->AsString;

Sheet.OlePropertyGet("Cells",iRows,iCols).OlePropertySet("Value",

strRowTemp.c_str());

}

file:

//画该行的表格边框

strRange="A"+IntToStr(iRows)+":

F"+IntToStr(iRows);

ERange=Sheet.OlePropertyGet("Range",strRange.c_str());

EBorders=ERange.OlePropertyGet("Borders");

EBorders.OlePropertySet("linestyle",xlContinuous);

EBorders.OlePropertySet("weight",xlThin);

EBorders.OlePropertySet("colorindex",xlAutomatic);

iRows++;

Query1->Next();

}//while结束

Wb.OleProcedure("Save");//保存表格

Wb.OleProcedure("Close");关闭表格

Ex.OleFunction("Quit");退出Excel

file:

//定义目标文件名

AnsiStringDestinationFile=

GetCurrentDir()+"\\report\\table.xls";

file:

//将刚刚修改的Excel表格文件table.xls拷贝到report目录下

if(!

CopyFile(ExcelFileName.c_str(),DestinationFile.c_str(),false))

{

Application->MessageBox("复制文件操作失败,Excel文件可能正在使用中!

",

"错误",MB_ICONSTOP|MB_OK);

return;

}

Application->MessageBox("成功完成报表保存!

\n可以按\'打开Excel文件\'

按钮进行报表工作","提示",MB_ICONINFORMATION|MB_OK);

SaveButton->Enabled=true;

ReadButton->Enabled=true;

}//try结束

catch(...)

{

Application->MessageBox("操作Excel表格失败!

",

"错误",MB_ICONSTOP|MB_OK);

Wb.OleProcedure("Close");

Ex.OleFunction("Quit");

SaveButton->Enabled=true;

ReadButton->Enabled=false;

}

}  

  至此,完成报表数据的写入工作。

如果要对完成的Excel表进行操作,可以点击"打开Excel表文件按钮"(ReadButton),进行修改,保存,打印等操作。

ReadButton的单击事件如下实现:

void__fastcallTForm1:

:

ReadButtonClick(TObject*Sender)

{

try

{

file:

//指定report目录下的报表文件用于用户操作

AnsiStringExcelFileName=  

GetCurrentDir();+"\\report\\table.xls";

if(!

FileExists(ExcelFileName))

{

Application->MessageBox("Excel表文件不存在,无法打开!

",

"错误",MB_ICONSTOP|MB_OK);

return;

}

try

{

Ex=Variant:

:

CreateObject("Excel.Application");

}

catch(...)

{

Application->MessageBox("无法启动Excel","错误",MB_ICONSTOP|MB_OK);

return;

}

file:

//使Excel可见

Ex.OlePropertySet("Visible",true);

file:

//打开Excel表格文件Table.xls

Ex.OlePropertyGet("WorkBooks").OleProcedure("Open",ExcelFileName.c_str());

}

catch(...)

{

Application->MessageBox("操作Excel表格错误!

","错误",MB_ICONSTOP|MB_OK);

Ex.OleFunction("Quit");

}

}

 

本文来自CSDN博客,转载请标明出处:

 ********************************************************************************************************************

//---把数据导出到Excel表中---

 try

 {

 BitBtn10->Enabled=false;

 Stringfilename1,filename2,strXlsFile;

 intpagenum;

 filename1="";

 //SaveD->FileEditStyle="";

 if(SaveD->Execute())

 {

 filename1=SaveD->FileName;//+".xls";

 }

 else

 {

 filename1="d:

\\daw50报表.xls";

 }

 if(filename1!

="");

 {

 if(!

DBGrid4->DataSource->DataSet->Active)//数据集没有打开就返回

 return;

 //表格的行数

 DBGrid4->DataSource->DataSet->Last();

 intnRowCount(DBGrid4->DataSource->DataSet->RecordCount+1);

 nRowCount=nRowCount<2?

2:

nRowCount;

 //表格的列数

 intnColCount(39);

 nColCount=nColCount<1?

1:

nColCount;

 intpnum=15000;//530

 intfilenum=nRowCount/pnum+1;

 DBGrid4->DataSource->DataSet->First();

 for(inthlpnum=1;hlpnum<=filenum;hlpnum++)

 {

 if(hlpnum

 pagenum=pnum;

 else

 pagenum=nRowCount-(hlpnum-1)*pnum-1;

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

 filename2=filename1+"0"+IntToStr(hlpnum);

 strXlsFile=filename2;

 //==============

 FILE*ofp;

 TStringList*HTMList;

 intFieldType[10]={0,0,0,0,2,0,0,0};

 boolret;

 char*sFileName;

 charFileName[200];

 sFileName=filename2.c_str();//"./mytest.csv";

 sprintf(FileName,"%s.xls\0",sFileName);

 ofp=fopen(FileName,"a+");

 if(ofp==NULL){

 ShowMessage("文件无法创建!

");

 return;

 }

 fputs("

solid'>\n",ofp);

 fflush(ofp);

 fputs("\n",ofp);

 //ss="路段名称";

 //fprintf(ofp,"%s\n",ss.c_str());

 ss="路段名称:

";

 fprintf(ofp,"%s\n",ss.c_str());

 fputs("\n",ofp);

 fflush(ofp);

 fputs("\n",ofp);

 for(intj=0;j<10;j++)

 {

 //标题行的行高

 //Stringss;

 switch(j)

 {

 case0:

 ss="编号";

 break;

 case1:

 ss="时间";

 break;

 case2:

 ss="车牌号码";

 break;

 case3:

 ss="总重";

 break;

 case4:

 ss="限重";

 break;

 case5:

 ss="超重";

 break;

 case6:

 ss="速度";

 break;

 case7:

 ss="人工车型";

 break;

 case8:

 ss="自动车型";

 break;

 case9:

 ss="轴数";

 break;

 case10:

 ss="行使里程";

 break;

 case11:

 ss="实际收费";

 break;

 }

 fprintf(ofp,"%s\n",ss.c_str());

 }

 fputs("\n",ofp);

 for(inti=0;i

 {

 //------读取数据-------------------

 vehno=DBGrid4->DataSource->DataSet->FieldByName("vehNo")->AsInteger;

 Dtime=DBGrid4->DataSource->DataSet->FieldByName("datetime")->AsString;

 vehid=DBGrid4->DataSource->DataSet->FieldByName("vehid")->AsString;

 axlenum=DBGrid4->DataSource->DataSet->FieldByName("TotalNum")->AsInteger;

 //grnum=DBGrid4->DataSource->DataSet->FieldByName("AXleNum")->AsInteger;

 Totalweight=DBGrid4->DataSource->DataSet->FieldByName("Totalweigh")->AsInteger;

 //overld=DBGrid4->DataSource->DataSet->FieldByName("overld")->AsInteger;

 speed=DBGrid4->DataSource->DataSet->FieldByName("speed")->AsInteger;

 autotype=DBGrid4->DataSource->DataSet->FieldByName("autotype")->AsString;

 handtype=DBGrid4->DataSource->DataSet->FieldByName("handtype")->AsString;

 //ts=DBGrid4->DataSource->DataSet->FieldByName("VEHTYPE")->AsString;

 if(axlenum>=2&&axlenum<6)

 limitweight=Newlimit[axlenum-2];

 else

 limitweight=Newlimit[4];

 overld=0;

 if(Totalweight>limitweight)

 overld=Totalweight-limitweight;

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

 fflush(ofp);

 fputs("\n",ofp);

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

 ss=IntToStr(vehno);

 fprintf(ofp,"%s\n",ss.c_str());

 ss=Trim(Dtime);

 fprintf(ofp,"%s\n",ss.c_str());

 ss=Trim(vehid);

 fprintf(ofp,"%s\n",ss.c_str());

 ss=IntToStr(Totalweight);

 fprintf(ofp,"%s\n",ss.c_str());

 ss=IntToStr(limitweight);

 fprintf(ofp,"%s\n",ss.c_str());

 ss=IntToStr(overld);

 fprintf(ofp

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 高等教育 > 文学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1