我现在采用excelapplication,excelworkbook,excelworksheet的模式进行数据转存,速度好慢,请高手帮忙,有没有办法改进?盼复,感激不尽!

解决方案 »

  1.   

    直接使用sql server的DTS服务
      

  2.   

    不能用DTS,我是编客户端的程序,要将查询的结果转存为excel文件。
      

  3.   

    1 你可以在服务器端用存储过程生成并将excel文件传出
    2 你可以在delphi中用ado连接excel文档直接进行操作
      

  4.   

    EXCEL文件是存在客户端的
    楼上能否说具体些?
      

  5.   

    就是要实现这个功能:客户将查询的结果(在ADOQUERY中)转存为EXCEL文件,EXCEL文件的保存路径、文件名由客户指定。我采用excelapplication,excelworkbook,excelworksheet已经实现了这个功能,但发现速度好慢,所以想请教专家有什么办法改进?
      

  6.   

    1.建立一个Excel实例
     2.创建一个工作表
     3.创建一个TStringList,用来装数据
     4.穷举数据表,将数据存入TStringList
     5.将TStringList的数据复制到剪贴板
     6.把剪贴板中的数据粘贴到Excel
      

  7.   

    直接按Excel的文件格式往Excel文件中写数据。
    下面是我在网上找到的一个例子:unit XLSFile;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Grids,
      Forms, Dialogs,db,dbctrls,comctrls;const
    {BOF}
      CBOF      = $0009;
      BIT_BIFF5 = $0800;
      BOF_BIFF5 = CBOF or BIT_BIFF5;
    {EOF}
      BIFF_EOF = $000a;
    {Document types}
      DOCTYPE_XLS = $0010;
    {Dimensions}
      DIMENSIONS = $0000;type
      TAtributCell = (acHidden,acLocked,acShaded,acBottomBorder,acTopBorder,
                    acRightBorder,acLeftBorder,acLeft,acCenter,acRight,acFill);  TSetOfAtribut = set of TatributCell;  TXLSWriter = class(Tobject)
      private
        fstream:TFileStream;
        procedure WriteWord(w:word);
      protected
        procedure WriteBOF;
        procedure WriteEOF;
        procedure WriteDimension;
      public
        maxCols,maxRows:Word;
        procedure CellWord(vCol,vRow:word;aValue:word;vAtribut:TSetOfAtribut=[]);
        procedure CellDouble(vCol,vRow:word;aValue:double;vAtribut:TSetOfAtribut=[]);
        procedure CellStr(vCol,vRow:word;aValue:String;vAtribut:TSetOfAtribut=[]);
        procedure WriteField(vCol,vRow:word;Field:TField);
        constructor create(vFileName:string);
        destructor destroy;override;
      end;procedure SetCellAtribut(value:TSetOfAtribut;var FAtribut:array of byte);
    procedure DataSetToXLS(ds:TDataSet;fname:String);
    procedure StringGridToXLS(grid:TStringGrid;fname:String);implementationprocedure DataSetToXLS(ds:TDataSet;fname:String);
    var c,r:Integer;
      xls:TXLSWriter;
    begin
      xls:=TXLSWriter.create(fname);
      if ds.FieldCount > xls.maxcols then
        xls.maxcols:=ds.fieldcount+1;
      try
        xls.writeBOF;
        xls.WriteDimension;
        for c:=0 to ds.FieldCount-1 do
          xls.Cellstr(0,c,ds.Fields[c].FieldName);
        r:=1;
        ds.first;
        while (not ds.eof) and (r <= xls.maxrows) do begin
          for c:=0 to ds.FieldCount-1 do
            xls.WriteField(r,c,ds.Fields[c]);
          inc(r);
          ds.next;
        end;
        xls.writeEOF;
      finally
        xls.free;
      end;
    end;procedure StringGridToXLS(grid:TStringGrid;fname:String);
    var c,r,rMax:Integer;
      xls:TXLSWriter;
    begin
      xls:=TXLSWriter.create(fname);
      rMax:=grid.RowCount;
      if grid.ColCount > xls.maxcols then
        xls.maxcols:=grid.ColCount+1;
      if rMax > xls.maxrows then
        rMax:=xls.maxrows;
      try
        xls.writeBOF;
        xls.WriteDimension;
        for c:=0 to grid.ColCount-1 do
          for r:=0 to rMax-1 do
            xls.Cellstr(r,c,grid.Cells[c,r]);
        xls.writeEOF;
      finally
        xls.free;
      end;
    end;{ TXLSWriter }constructor TXLSWriter.create(vFileName:string);
    begin
      inherited create;
      if FileExists(vFilename) then
        fStream:=TFileStream.Create(vFilename,fmOpenWrite)
      else
        fStream:=TFileStream.Create(vFilename,fmCreate);  maxCols:=100;   //
      maxRows:=65535; //
    end;destructor TXLSWriter.destroy;
    begin
      if fStream <> nil then
        fStream.free;
      inherited;
    end;procedure TXLSWriter.WriteBOF;
    begin
      Writeword(BOF_BIFF5);
      Writeword(6);           // count of bytes
      Writeword(0);
      Writeword(DOCTYPE_XLS);
      Writeword(0);
    end;procedure TXLSWriter.WriteDimension;
    begin
      Writeword(DIMENSIONS);  // dimension OP Code
      Writeword(8);           // count of bytes
      Writeword(0);           // min cols
      Writeword(maxRows);     // max rows
      Writeword(0);           // min rowss
      Writeword(maxcols);     // max cols
    end;procedure TXLSWriter.CellDouble(vCol, vRow: word; aValue: double;
      vAtribut: TSetOfAtribut);
    var  FAtribut:array [0..2] of byte;
    begin
      Writeword(3);           // opcode for double
      Writeword(15);          // count of byte
      Writeword(vCol);
      Writeword(vRow);
      SetCellAtribut(vAtribut,fAtribut);
      fStream.Write(fAtribut,3);
      fStream.Write(aValue,8);
    end;procedure TXLSWriter.CellWord(vCol,vRow:word;aValue:word;vAtribut:TSetOfAtribut=[]);
    var  FAtribut:array [0..2] of byte;
    begin
      Writeword(2);           // opcode for word
      Writeword(9);           // count of byte
      Writeword(vCol);
      Writeword(vRow);
      SetCellAtribut(vAtribut,fAtribut);
      fStream.Write(fAtribut,3);
      Writeword(aValue);
    end;procedure TXLSWriter.CellStr(vCol, vRow: word; aValue: String;
      vAtribut: TSetOfAtribut);
    var  FAtribut:array [0..2] of byte;
      slen:byte;
    begin
      Writeword(4);           // opcode for string
      slen:=length(avalue);
      Writeword(slen+8);      // count of byte
      Writeword(vCol);
      Writeword(vRow);
      SetCellAtribut(vAtribut,fAtribut);
      fStream.Write(fAtribut,3);
      fStream.Write(slen,1);
      fStream.Write(aValue[1],slen);
    end;procedure SetCellAtribut(value:TSetOfAtribut;var FAtribut:array of byte);
    var
       i:integer;
    begin
     //reset
      for i:=0 to High(FAtribut) do
        FAtribut[i]:=0;     {Byte Offset     Bit   Description                     Contents
         0          7     Cell is not hidden              0b
                          Cell is hidden                  1b
                    6     Cell is not locked              0b
                          Cell is locked                  1b
                    5-0   Reserved, must be 0             000000b
         1          7-6   Font number (4 possible)
                    5-0   Cell format code
         2          7     Cell is not shaded              0b
                          Cell is shaded                  1b
                    6     Cell has no bottom border       0b
                          Cell has a bottom border        1b
                    5     Cell has no top border          0b
                          Cell has a top border           1b
                    4     Cell has no right border        0b
                          Cell has a right border         1b
                    3     Cell has no left border         0b
                          Cell has a left border          1b
                    2-0   Cell alignment code
                               general                    000b
                               left                       001b
                               center                     010b
                               right                      011b
                               fill                       100b
                               Multiplan default align.   111b
         }     //  bit sequence 76543210     if  acHidden in value then       //byte 0 bit 7:
             FAtribut[0] := FAtribut[0] + 128;     if  acLocked in value then       //byte 0 bit 6:
             FAtribut[0] := FAtribut[0] + 64 ;     if  acShaded in value then       //byte 2 bit 7:
             FAtribut[2] := FAtribut[2] + 128;     if  acBottomBorder in value then //byte 2 bit 6
             FAtribut[2] := FAtribut[2] + 64 ;     if  acTopBorder in value then    //byte 2 bit 5
             FAtribut[2] := FAtribut[2] + 32;     if  acRightBorder in value then  //byte 2 bit 4
             FAtribut[2] := FAtribut[2] + 16;     if  acLeftBorder in value then   //byte 2 bit 3
             FAtribut[2] := FAtribut[2] + 8;     if  acLeft in value then         //byte 2 bit 1
             FAtribut[2] := FAtribut[2] + 1
         else if  acCenter in value then  //byte 2 bit 1
             FAtribut[2] := FAtribut[2] + 2
         else if acRight in value then    //byte 2, bit 0 dan bit 1
             FAtribut[2] := FAtribut[2] + 3
         else if acFill in value then     //byte 2, bit 0
             FAtribut[2] := FAtribut[2] + 4;
    end;procedure TXLSWriter.WriteWord(w: word);
    begin
      fstream.Write(w,2);
    end;procedure TXLSWriter.WriteEOF;
    begin
      Writeword(BIFF_EOF);
      Writeword(0);
    end;procedure TXLSWriter.WriteField(vCol, vRow: word; Field: TField);
    begin
      case field.DataType of
         ftString,ftWideString,ftBoolean,ftDate,ftDateTime,ftTime:
           Cellstr(vcol,vrow,field.asstring);
         ftAutoInc,ftSmallint,ftInteger,ftWord:
           CellWord(vcol,vRow,field.AsInteger);
         ftFloat, ftBCD:
           CellDouble(vcol,vrow,field.AsFloat);
      else
           Cellstr(vcol,vrow,EmptyStr);
      end;
    end;
    end.
      

  8.   

    调用
    ///
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Db, DBTables, Grids, DBGrids,xlsfile;type
      TForm1 = class(TForm)
        DBGrid1: TDBGrid;
        Table1: TTable;
        DataSource1: TDataSource;
        Button1: TButton;
        SaveDialog1: TSaveDialog;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
    var
      t1, t2: TDateTime;
    begin
      if SaveDialog1.Execute then
      begin
        t1 := time;
        DataSetToXLS(dbgrid1.DataSource.dataset,SaveDialog1.filename);
        t2 := time;
        Caption := Format('%d , %d',
          [dbgrid1.DataSource.dataset.RecordCount,
           trunc((t2 - t1)*24*60*60*1000)]);
      end;
    end;end.
      

  9.   

    有关文件格式的站点: http://www.wotsit.org/
    是我找到的文件格式最全的站点。
      

  10.   

    对不起我没说清我要实现的功能,是这样的:用ADOQUERY查询的结果显示在DBGRID中,但我对DBGRID的某些列进行了设置,有的列的VISIBLE是FALSE,在导出到EXCEL时只导出列的VISIBLE是TURE的数据。
      

  11.   

    // 老大,你就加一个处理 DBGrid 的过程就可以了,如下
    procedure DBGridToXLS(grid:TDBGrid; fname:String);
    var i,c,r:Integer;
      ds: TDataSet;
      xls:TXLSWriter;
    begin
      xls:=TXLSWriter.create(fname);
      if grid.Columns.Count > xls.maxcols then
        xls.maxcols:=grid.Columns.Count+1;
      try
        xls.writeBOF;
        xls.WriteDimension;
        c := 0;
        for i:=0 to grid.Columns.Count-1 do
          if grid.Columns[i].Visible then
          begin
            xls.Cellstr(0,c,grid.Columns[i].Title.Caption);
            c := c + 1;
          end;    r:=1;
        ds := grid.DataSource.DataSet;
        ds.First;
        while (not ds.eof) and (r <= xls.maxrows) do begin
          c := 0;
          for i:=0 to grid.Columns.Count-1 do
            if grid.Columns[i].Visible then
            begin
              xls.WriteField(r,c,ds.FieldByName(grid.Columns[i].FieldName));
              c := c + 1;
            end;      inc(r);
          ds.next;
        end;
        xls.writeEOF;
      finally
        xls.free;
      end;
    end;
      

  12.   

    用word宏的功能
    看看能不能实现
      

  13.   

    我试了一下jingpingyi的方法,真的导出好快,谢谢!马上给分!