求Delphi访问SQL2000数据库,将某个表数据查询出来/查询结果导出EXCEL

解决方案 »

  1.   

    上google搜索数据库导出excel。
    思路很简单,把excel当作数据源,两个数据源之间导数据不难吧?
      

  2.   

    一。笨点的方法就查询数据 对数据集循环读取值,往EXCEL里一个个写数据
    二。用OpenDataSource或者BCP命令直接导出
      

  3.   

    uses
       ...ExcelXP,ComObj;//////////////////////////
    var 
    Eclapp,WorkBook:variant;
    i,n,s:integer;
    xlsFileName:string;
    begin
      xlsFileName:=extractfilepath(application.ExeName)+'temp.xls';   //导出至本程序目录下
      if not cx5.Active then exit;
      if cx5.RecordCount<=0 then exit;
      if application.MessageBox('确认导出excel表吗?','提示',mb_okcancel+mb_iconinformation)=idcancel then exit;
      Eclapp:=createoleobject('Excel.Application');
      WorkBook:=CreateOleobject('Excel.Sheet');
      workBook:=Eclapp.workbooks.add;
      for i:=0 to DBGrid1.FieldCount-1 do
      begin
      Eclapp.cells[1,i+1]:=dbgrid1.Columns[i].Title.Caption;
      end;
      cx5.First;
      n:=2;
      while not cx5.Eof do
      for s:=0 to cx5.RecordCount do
      begin
      eclapp.cells[n,1] :=cx5.Fields[0].AsString;//导出几项加几个
      eclapp.cells[n,2] :=''''+cx5.Fields[1].AsString;//文本格式时加''''
      inc(n);
      cx5.Next;
      end;
      WorkBook.saveas(xlsFileName);
      WorkBook.close;
      eclApp.Quit;
      eclApp:=Unassigned;
    end;
      

  4.   

    用cxGrid来显示,然后导出吧!速度好快的!
      

  5.   

    如果不想换用第三方控件的话,就可以采用数据遍历的方法,将数据导出来
    建议换用三方控件,如dbgrideh,cxGrid等等.不但让你现在的需要轻松实现,还有很多你意想不到的功能
      

  6.   

    con1 .Execute('select * into hjp in "'+'savedialoge.filename'+'.xls" "excel 8.0;" from customer'); 动态保存,动态命名
    使用 dialoge控件con1 .Execute('select * into hjp in "'+'c:\'+'雇员表.xls" "excel 8.0;" from customer');指定固定路径,固定命名
    hjp是库名, 中文是文件名,customer表名 
    con1.Execute ('SELECT company INTO [我爱1Delphi.htm] IN "' + 'c:\' + '" "HTML Export;" FROM CUSTOMER');指定固定路径,固定命名
      

  7.   

    如果用第三方控件,推荐advstringgrid,比stringgrid多许多功能,先用adv显示数据查看正确性,再一句话就可以保存了,形式如下:advstringgrid1.savetoxls(文件名)就好了