引用楼主 haowenyao 的回复:
我正在用c++builder编写一个程序,想将数据库中的记录导出到excel中。
请问用c++builder中的什么控件?代码如何写?
//---把数据导出到Excel表中---
try
{
BitBtn10->Enabled=false;
String filename1,filename2,strXlsFile;
int pagenum;
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();
int nRowCount(DBGrid4->DataSource->DataSet->RecordCount + 1);
nRowCount = nRowCount < 2? 2: nRowCount;
// 表格的列数
int nColCount(39);
nColCount = nColCount < 1? 1: nColCount;
int pnum=15000;// 530
int filenum= nRowCount/pnum+1;
DBGrid4->DataSource->DataSet->First();
for(int hlpnum=1;hlpnum<=filenum;hlpnum++)
{
if(hlpnum<filenum)
pagenum=pnum;
else
pagenum=nRowCount-(hlpnum-1)*pnum-1;
//-------------------------------------
filename2=filename1+"0"+IntToStr(hlpnum);
strXlsFile=filename2;
//==============
FILE * ofp;
TStringList *HTMList;
int FieldType[10]={0,0,0,0,2,0,0,0};
bool ret;
char *sFileName;
char FileName[200];
sFileName=filename2.c_str();//"./mytest.csv";
sprintf(FileName,"%s.xls\0", sFileName);
ofp=fopen(FileName,"a+");
if(ofp==NULL){
ShowMessage("文件无法创建!!");
return;
}
fputs("<table border='1' style='border-style: solid'>\n", ofp);
fflush(ofp);
fputs("<tr height=24 >\n", ofp);
//ss="路段名称";
// fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss="路段名称:";
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
fputs("</tr>\n", ofp);
fflush(ofp);
fputs("<tr height=24 >\n", ofp);
for(int j=0; j<10; j++)
{
// 标题行的行高
//String ss;
switch(j)
{
case 0:
ss="编号";
break;
case 1:
ss="时间";
break;
case 2:
ss="车牌号码";
break;
case 3:
ss="总重";
break;
case 4:
ss="限重";
break;
case 5:
ss="超重";
break;
case 6:
ss="速度";
break;
case 7:
ss="人工车型";
break;
case 8:
ss="自动车型";
break;
case 9:
ss="轴数";
break;
case 10:
ss="行使里程";
break;
case 11:
ss="实际收费";
break;
}
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
}
fputs("</tr>\n", ofp);
for(int i=0; i<pagenum; 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("<tr height=24 >\n", ofp);
//---------------------------
ss=IntToStr(vehno);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss=Trim(Dtime);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss=Trim(vehid);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss=IntToStr(Totalweight);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss=IntToStr(limitweight);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss=IntToStr(overld);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss=IntToStr(speed);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss= Trim(handtype);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss= Trim(autotype);
if(ss=="类车")ss="五类车";
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
ss=IntToStr(axlenum);
fprintf(ofp, "<td height=24 >%s</td>\n", ss.c_str());
//-------------
//--下一行---
DBGrid4->DataSource->DataSet->Next();
}
fputs("</table>\n", ofp);
fclose(ofp);
} //for for
}//for if
BitBtn10->Enabled=true;
}
catch(...)
{
BitBtn10->Enabled=true;
}