把数据导入Excel的方法很多,其中写Excel单元格是最常见的方法,但速度太慢。
用ADO连接是一种易于理解,速度上也可以接受的一种方法: 
   其中 select   *   into   [Excel   8.0;database=c:\temp\1.xls].mytable   from   mytable 就可以实现
   但 以前还看过一种方法,类似下面:
   ExcelApp:=CreateOleObject('Excel.Application');
   //省略
   ExcelApp.WorkSheets['用户信息'].CopyFromDataSet(ADOQuery);  
   最后是比较关键的一句,但现在连编译都通不过。   1:请各位 给一个该 方法的完善版本!!! (本人比较倾向于这种方法)   2:另外 像这种与 Excel和Word等office组件交互编程的书籍,
   请各位推荐几本,c++ 或 delphi 的都可以。 

解决方案 »

  1.   

    你稍微在CSDN搜索下相关的 能找出一大堆!根本都不用开贴问。
      

  2.   

    给你个函数:
    class function ExportExcel.funExportExcel(DsData: TDataSet; strFileName,
      strTitle: string): boolean;
    var
      intRow,intColumn:integer;
      intColLoop:integer;
      blReturn:boolean;
      excelapp: TExcelApplication;
      workbook: TExcelWorkbook;
      worksheet: TExcelWorksheet;
      bkDS:TBook;
      vMerge:variant;
    begin
      if DsData.State=dsInactive then
      begin
        Result:=false;
        Exit;
      end;
      
      DsData.DisableControls;
      bkDS:=DsData.GetBook;
      excelapp:=TExcelApplication.Create(nil);
      workbook:= TExcelWorkbook.Create(nil);
      worksheet:= TExcelWorksheet.Create(nil);
      try
        excelapp.Connect;
        excelapp.Workbooks.Add(xlWBATWorksheet,0);
        workbook.ConnectTo(excelapp.Workbooks.Item[1]);
        worksheet.ConnectTo(workbook.Worksheets[1] as _worksheet);
        with worksheet do
        begin
          intColumn:=DsData.FieldCount;
          cells.Item[1,(intcolumn div 2)+1]:=strTitle;
          cells.Item[2,(intcolumn div 2)+1]:='总记录数:'+inttostr(DsData.RecordCount);
          
          for intColLoop:=0 to intColumn -1 do
          begin
            cells.Item[3,intColLoop+1]:=DsData.fields[intColLoop].DisplayLabel;
          end;
          Range[cells.Item[1,1],cells.Item[1,intColumn]].Font.Size:=18;
          Range[cells.Item[1,1],cells.Item[1,intColumn]].Font.Name:='隶书';
          Range[cells.Item[1,1],cells.Item[1,intColumn]].Font.Bold:=true;
          Range[cells.Item[1,1],cells.Item[1,intColumn]].Merge(vMerge);
          Range[cells.Item[2,1],cells.Item[2,intColumn]].Font.Size:=14;
          Range[cells.Item[2,1],cells.Item[2,intColumn]].Font.Bold:=true;
          Range[cells.Item[2,1],cells.Item[2,intColumn]].Merge(vMerge);
          Range[cells.Item[3,1],cells.Item[2,intColumn]].Font.Size:=12;
          //
          Range[cells.Item[1,1],cells.Item[3,intColumn]].HorizontalAlignment:=xlCenter;
          Range[cells.Item[1,1],cells.Item[3,intColumn]].VerticalAlignment:=xlCenter;
          //
          with dsData do
          begin
            First;
            intRow:= 1;
            while not Eof do
            begin
              for intColLoop:=0 to intColumn-1 do
              begin
                  cells.Item[intRow+3,intColLoop+1]:=fields[intColLoop].AsString;
              end;
              introw:=intRow+1;
              next;
            end;
          end;
          cells.Item[introw+3,1]:='打印日期:'+datetostr(date);
          Range[cells.Item[introw+3,1],cells.Item[introw+3,intColumn]].Merge(vMerge);
          Columns.AutoFit;      if fileexists(strFileName) then
            deletefile(strFileName);
          saveas(strFileName);
        end;
        blReturn:=true;
      finally
        excelapp.Disconnect;
        excelapp.Quit;
        excelapp.Free;
        workbook.Free;
        worksheet.Free;
        DsData.GotoBook(bkDS);
        DsData.EnableControls;
      end;
      Result:=blReturn;
    end;
      

  3.   

    DsData.DisableControls; 
      bkDS:=DsData.GetBook; 
      excelapp:=TExcelApplication.Create(nil); 
      workbook:= TExcelWorkbook.Create(nil); 
      worksheet:= TExcelWorksheet.Create(nil); 
      

  4.   

    基本上主要用两种,一种是Ole,就是你写的这种方法,还有一种是数据流的方法(速度快),CSDN里面都有
      

  5.   

      上面所说的方法都不是最快的.  最快的是将数据整块的发送到剪贴板进行粘贴.速度快了几十倍!  如果是从excel往外读数据.我有一个方法,它的速度是单元格读写的几百倍.具体的,你自己研究吧.我这里的1个excel报告文件中共1万3千条记录,原来读出来要3分钟,现在改进后,只需2秒钟就可以读出来了.
      

  6.   

    procedure Tfrm_export.bt_exprotClick(Sender: TObject);
    var
     i,j,row,nall:integer; 
    begin
        if SaveDialog1.Execute then
        begin
            frm_proc.Show;
            ExcelApplication1.Connect;
            ExcelApplication1.Workbooks.Add(Null,0);
            ExcelWorkBook1.ConnectTo(ExcelApplication1.Workbooks[1]);
            ExcelWorkSheet1.ConnectTo(ExcelWorkBook1.Sheets[1] as _WorkSheet);
            application.ProcessMessages;
        end else
        begin
            exit;
        end;    ExcelApplication1.Rows.RowHeight := 15;
        ExcelApplication1.Columns.AutoFit;
        ExcelApplication1.Columns.ColumnWidth := 20;    ExcelApplication1.Cells.Font.Name := 'Courier New';
        ExcelApplication1.Cells.Font.Color := clBlack;
        ExcelApplication1.Cells.Font.size := 9;    ExcelApplication1.Cells.HorizontalAlignment := 3;
        ExcelApplication1.Cells.VerticalAlignment := 2;
         nall:=qry_export.RecordCount;
         row:=0;
         for j:=0 to qry_export.Fields.Count-1 do
         begin
              ExcelWorksheet1.Cells.item[1, j+1] := qry_export.Fields[j].DisplayName  //.DisplayLabel;
         end;
         for i:=0 to nall-1 do
         begin
             while not query_export.Eof do
             begin
                  for j:=0 to query_export.Fields.Count-1 do       // column逐列导出动作
                begin
                    ExcelWorkSheet1.Cells.Item[row+2,j+1]:=string(query_export.Fields[j].AsString);
                end;
                 row:=row+1;
                 query_export.Next;
             end;
         end;
          ExcelWorkBook1.SaveCopyAs(SaveDialog1.FileName+'.xls');
          ExcelWorkBook1.Close(false);
          ExcelApplication1.Disconnect;
          Screen.Cursor:=crDefault;
          frm_proc.Close; 
          showmessage('成功导出 '+inttostr(nall)+' 条数据!');end;以上是导出已经找开的一个数据住集的数据到EXCEL中,已经测试,复制即可.
      

  7.   

    procedure TfmKT_DayReport_Main.ExportToExcel(Dbgd:Tdbgrid;CellX:integer;CellY:integer;Save:Boolean);
    var  //cellx和celly是指从哪个单格开始写数据。Save是写完数据后是否保存
      i,hang,lie:integer;
      Datas:variant;
      Pdbset:^Tadodataset;
    begin
      try
        //StatusBar1.Panels.Items[1].Text:='正在导出数据到Excel....';
        PDBset:[email protected];
        hang:=1;
        lie:=pdbset.FieldCount;
        Datas:=varArrayCreate([1,PDBset.RecordCount,1,pdbset.FieldCount],varVariant);
        if Pdbset.RecordCount>0 then
        begin
          Pdbset.First;
          Pdbset.DisableControls;
          while not Pdbset.Eof do
          begin
            lie:=0;
            inc(hang);
            for I := 0 to pdbset.FieldCount - 1 do
            begin
              inc(lie);
              datas[hang-1,lie]:=pdbset.Fields[i].AsString;
            end;
            Pdbset.Next;
          end;
          Exsheet.range[exsheet.cells[CellX,CellY],exsheet.cells[Pdbset.RecordCount+CellX-1,lie+CellY-1]].value:=Datas;
          Exsheet.range[exsheet.cells[CellX,CellY],exsheet.cells[Pdbset.RecordCount+CellX-1,lie+CellY-1]].select;
          ExApp.selection.NumberFormatLocal:='G/通用格式';
        end;
        if (DateTimePicker_EDate.Enabled=True)and(DateTimePicker_EDate.Date>DateTimePicker_BDate.Date) then
        begin
        SaveName:=Frame_DayreportTree1.GetExportPath+FormatDateTime('mm月dd日',DateTimePicker_BDate.Date)+'-'+
                  FormatDateTime('mm月dd日',DateTimePicker_EDate.Date)+
                  Frame_DayreportTree1.GetExportName+'.xls';
        end;
        if Save then
          ExBook.Saveas(SaveName);
      finally
        Pdbset.EnableControls;
      end;
    end;这是我用的代码,感觉速度还比较快。不过这只是其中主要的一部分,你要用的话还得自己改一下,比如创建excel,用完后销毁。其实这也是我从别的地方抄来的,不过自己改了一下。呵呵。