cb6如何将dbgrid里显示的结果导出成excel或txt文件?
用ADO查询SQL2000数据库,然后dbgrid控件显示出了结果。
现在就是想做一个按钮,单击后把dbgrid显示的结果自动保存到指定文件夹(如程序所在文件夹)。
请教大侠解决,谢了~
[解决办法]
不多说,上链接:
http://ccrun.com/article.asp?i=635&d=g75jbn
[解决办法]
建议用现成的控件,妖哥的办法是可以,但是如果数据量大的话速度很慢。EhLib不错的选择,还有个EasyGrid也可以
[解决办法]
我有个Tofficl类,一个.cpp文件加到工程里。生成个对象,调用个成员函数,设置下参数,给DBGrid对象或什么StringGrid对象进去,就存了,以前项目里做的,忘记放哪了,找找看
[解决办法]
void __fastcall TMain::BitBtn10Click(TObject *Sender){ int vehno,axlenum,Totalweight,grnum,limitweight,overld,speed; int axleweigh[8]; AnsiString ss,stemp,ts; String autotype,handtype,Dtime,vehid; char* data_buf; int i,j; int overaxle; int t[4]; String axletype[4]; //EditQClear(); axletype[0]="①"; axletype[1]="②"; axletype[2]="⑤"; axletype[3]="⑦"; //---把数据导出到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; }}