Application.CreateForm(TfrmProGress, frmProGress);
  frmProGress.Show;
  MaxRow := ads.RecordCount;
  frmProGress.lbl1.Caption := '0/' + IntToStr(MaxRow);
  progressstep := 100 / MaxRow;
  frmProGress.cxPsbpsb.Position := 0;
  ads.First;
  xj := ys + 1;
  for j := xj to ads.RecordCount + ys do
  begin
    for i := xs to ads.fieldcount - 3 + xs do
    begin
      rangs := crs[i] + inttostr(j);
      PackListToExcelFile.range[rangs].Font.bold := False;
      PackListToExcelFile.range[rangs].font.size := fs;
      PackListToExcelFile.range[rangs].Font.Name := 'Courier New';
      PackListToExcelFile.range[rangs].value := ads.fields[i - xs + 2].AsString;
    end;
    ads.Next;
    frmProGress.cxPsbpsb.Position :=
      frmProGress.cxPsbpsb.Position + progressstep;
    frmProGress.lbl1.Caption := IntToStr(j) + '/' + IntToStr(MaxRow);
    Application.ProcessMessages;
  end;

解决方案 »

  1.   

    你的这个循环做了太多多余的工作,所以速度会受很大的影响。
    最好的方法是
    使用ADOConncetion像数据库一样访问Excel表,往表中写数据。
      

  2.   

    怎么做?我没写过。请指教,就按上面那样,给你个dataset,然后呢?多谢
      

  3.   

    PackListToExcelFile.range[rangs].Font.bold := False;
          PackListToExcelFile.range[rangs].font.size := fs;
          PackListToExcelFile.range[rangs].Font.Name := 'Courier New';
          PackListToExcelFile.range[rangs].value := ads.fields[i - xs + 2].AsString;
    这段代码这样跑是不行的,前面三行完全可以拿到循环外面来,等你把数据写完了再统一进行排版
      

  4.   

    用DBGridEh设置一下,即可搞定,几万条数据一秒钟搞定,一下子就导出去了!
      

  5.   

    去看一下DBGRIDEH是如何导出EXCEL的...
      

  6.   

    网上搜一下,有一条SQL语句连接Excel
      

  7.   

    1。在生成时候不让EXCEL可见
    2。用PackListToExcelFile。range('a1').copyfromrecordset rs 跟快
    3。
      

  8.   

    SELECT * INTO YourTableName FROM 
    OPENROWSET(‘MSDASQL.1‘, ‘driver=Microsoft Excel Driver (*.xls);DBQ=e:tempbook2.xls‘,‘select * from [sheet1$]‘)==============================================================
    SELECT 故障归类,故障代码,故障描述 INTO code FROM 
    OPENROWSET(‘MSDASQL.1‘, ‘driver=Microsoft Excel Driver (*.xls);DBQ=c:\code.xls‘,‘select 故障归类,故障代码,故障描述 from [sheet1$]‘)
    第一种方法可能会导致5列数据只导出3列,把*写成所有具体的列名就可以避免这个问题拉!
      

  9.   

    参考:
    Excel联接ADO串
    Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\temp\a.xls;Extended Properties="Excel 8.0;HDR=Yes;";Persist Security Info=False
      

  10.   

    /////////////////////////////////////////////
    利用剪贴板,速度很快!适合装有Excel的机器
    USES Clipbrd,ComObj;procedure TForm1.Button1Click(Sender: TObject);
    var
      str:string;
      i:Integer;
      excelapp,sheet:Variant;
    begin
    //  lbl2.Caption:=DateTimeToStr(Now);
      str:='';
      dbgrd1.DataSource.DataSet.DisableControls;
      for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do
       str:=str+dbgrd1.DataSource.DataSet.fields[i].DisplayLabel+char(9);
      str:=str+#13;
      dbgrd1.DataSource.DataSet.First;
      while not(dbgrd1.DataSource.DataSet.eof) do begin
        for i:=0  to dbgrd1.DataSource.DataSet.FieldCount-1 do
         str:=str+dbgrd1.DataSource.DataSet.Fields[i].AsString+char(9);
        str:=str+#13;
        dbgrd1.DataSource.DataSet.next;    lbl1.Caption:=IntToStr(dbgrd1.DataSource.DataSet.RecNo);
        Application.ProcessMessages;
      
       end;//end while   dbgrd1.DataSource.DataSet.EnableControls;   clipboard.Clear;
       Clipboard.Open;
       Clipboard.AsText:=str;
       Clipboard.Close;
       excelapp:=createoleobject('excel.application');
       excelapp.workbooks.add(1); // excelapp.workbooks.add(-4167);
       sheet:=excelapp.workbooks[1].worksheets[1];
       sheet.name:='sheet1';
       sheet.paste;
       Clipboard.Clear;
    //   sheet.columns.font.Name:='宋体';
    //   sheet.columns.font.size:=9;
    //   sheet.Columns.AutoFit;
       excelapp.visible:=true;
    //   lbl3.Caption:=DateTimeToStr(Now);end;/////////////////////////////////////////////
      

  11.   

    ////////////////////////////////////////////////
    利用TStringList,速度很快!适合没有装Excel的机器
    procedure TForm1.Button1Click(Sender: TObject);
    var
      s:TStringList;
      str:string;
      i:Integer;
    begin
    //  lbl1.Caption:=DateTimeToStr(Now);
      str:='';
      dbgrd1.DataSource.DataSet.DisableControls;
      for i:=0 to dbgrd1.DataSource.DataSet.FieldCount-1 do
        str:=str+dbgrd1.DataSource.DataSet.fields[i].DisplayLabel+char(9);
      str:=str+#13;
      dbgrd1.DataSource.DataSet.First;
      while not(dbgrd1.DataSource.DataSet.eof) do begin
        for i:=0  to dbgrd1.DataSource.DataSet.FieldCount-1 do
          str:=str+dbgrd1.DataSource.DataSet.Fields[i].AsString+char(9);      str:=str+#13;
          dbgrd1.DataSource.DataSet.next;//    lbl3.Caption:=IntToStr(dbgrd1.DataSource.DataSet.RecNo);
    //    Application.ProcessMessages;   end;//end while   dbgrd1.DataSource.DataSet.EnableControls;
       s:=TStringList.Create;
       s.Add(str);
       s.SaveToFile('c:\temp.xls');//保存到c:\temp.xls
       s.Free;
    //   lbl2.Caption:=DateTimeToStr(Now);end;
    ////////////////////////////////////////////////
      

  12.   

    for i:=0 to TempQuery.FieldCount-1 do
             str:=str+ tempQuery.Fields[i].FieldName+#9;
         buffList.Add(str);     while not TempQuery.Eof do
         begin
                 str:='';
                 for i:=0 to TempQuery.FieldCount-1 do
                     str:=str+TempQuery.Fields[i].AsString+#9;
                 buffList.Add(str);
                 ProgressBar2.Position:=ProgressBar2.Position+1;
                 TempQuery.Next;
         end;
         buffList.SaveToFile('c:\aa.xls');不过这种方法生成的.xls文件并不是真正的excle文件,他只不过是个文本文件。
      

  13.   

    有兴趣可以看一下我的方法,用封装好的接口访问EXCEL。http://community.csdn.net/expert/forum.asp?url=/Expert/ForumList.asp?
    roomid=5301&typenum=1&xmlsrc=&whichpage=1
      

  14.   

    可能是这个地址吧,哈http://blog.csdn.net/swayi21/services/trackbacks/67087.aspx
      

  15.   

    http://www.somade.com/是个很专业的技术社区,去那里找找吧,或许有你要的答案~
      

  16.   

    很多人问这个问题,是这里,看过后请提建议,谢谢!http://blog.csdn.net/swayi21/archive/2004/08/06/67087.aspx