怎样把dbgrid中的数据一次性全部导入到excel表格中??在线等!希望5:30之前能解决!

解决方案 »

  1.   

    UpdateBANKS(eclApp,DM_CW.Query_WE3)
    我知道的是这个。但是具体意思我不懂。能解释一下吗?然后在告诉我其他的,谢谢
      

  2.   

    uses
      ComObj,Excel2000, ExtCtrls;
    procedure CopyDbDataToExcel(Target:TswDBGrid;Title:String;Day:String);
    var
      iCount,jCount:Integer;
      XLApp:Variant;
      Sheet: Variant;
      i:Integer;
      temp:String;
      Const
      HRang:array[1..23] of string=('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W');
    //  Target1:TDBGrid;
    begin
      Screen.Cursor := crHourGlass;
      if not VarIsEmpty(XLApp) then
      begin
        XLApp.DisplayAlerts  := False;
        XLApp.Quit;
        VarClear(XLApp);
      end;
      //籵徹ole斐膘Excel勤砓
      Try
        XLApp := CreateOleObject('Excel.Application');
      Except
        Screen.Cursor := crDefault;
        Exit;
      end;
      //xlApp.WorkBooks.open(ExtractFilePath(Application.ExeName)+'Pay.xls');
      XLApp.WorkBooks.Add[XLWBatWorksheet];
      XLApp.WorkBooks[1].WorkSheets[1].Name := 'Sheet1';
      Sheet := XLApp.Workbooks[1].WorkSheets['Sheet1'] ;
      if Not Target.DataSource.DataSet.Active then
      begin
        Screen.Cursor := crDefault;
        Exit;
      end;
      Sheet.range['B1'].value:=Title;
      Sheet.range['B1'].Font.Size:=18;
      Sheet.range['F1'].value:=Day;
      Target.DataSource.DataSet.first;
      for iCount := 0 to Target.Columns.Count -1 do
      begin
            Sheet.cells[2 ,iCount+1] := Target.Columns.Items[iCount].Title.Caption;
      end;
      jCount:=1;
      While Not Target.DataSource.DataSet.Eof do
      begin
         for iCount := 0 to Target.Columns.Count -1 do
         begin
            Sheet.cells[jCount+2 ,iCount+1] := Target.Columns.Items[iCount].Field.DisplayText;
         end;
         Inc(jCount);
         Target.DataSource.DataSet.Next;
      end;  Sheet.Range['A2:'+hrang[iCount]+'1'].Font.Bold := True;
      Sheet.Range['A2:'+hrang[iCount]+IntToStr(jCount+1)].Font.Name:='冼极';
      Sheet.Range['A2:'+hrang[iCount]+IntToStr(jCount+1)].Font.Size:=9;
      Sheet.Range['A2:'+hrang[iCount]+IntToStr(jCount+1)].Borders.LineStyle:=1;
      Sheet.Range['A2:'+hrang[iCount]+IntToStr(jCount+1)].Columns.AutoFit;
      Sheet.Range['A2:'+hrang[iCount]+IntToStr(jCount+1)].HorizontalAlignment:=xlVAlignCenter;
      Sheet.Range['A2:'+hrang[iCount]+IntToStr(jCount+1)].AddIndent := True;//  Sheet.PageSetup.Orientation := xlLandscape;
      XlApp.Visible := True;
      Screen.Cursor := crDefault;
    end; 
    来自:gzfrank 时间:2002-5-17 11:51:00 ID:1106355 用dxDbGrid很方便,速度很快,2000条记录不用1s 
    来自:linsb 时间:2002-5-17 12:01:00 ID:1106382 有三种办法:
    1.ole或Savers
    2.ado
    3.第三方控件
     
    来自:nathanlee 时间:2002-5-17 12:09:00 ID:1106404 function TPubData.OutPutDBF(Src:TDBDataSet;SrcGrid:TDBGrid; TrgName: string): Boolean;
    var
      elapp,workbook:variant;
      xelfileName: string;
      I,J: integer;
    begin
      xelfilename :='c:\temp\'+TrgName+'.xls';
      try
        elapp :=CreateOLEObject('Excel.Application');//;
        workbook := CreateOLEObject('Excel.Sheet');
        workbook := Elapp.workbooks.add;
        For I := 0 to Src.FieldCount - 2 do
          elapp.cells(1,I+1) := SrcGrid.Columns[I].Title.Caption;
        With Src do
        begin
          DisableControls;
          First;
          J := 2;
          While not eof do
          begin
            For I := 1 to Src.FieldCount do
              elapp.cells(J,I) := Fields[I-1].AsString;
            Next;
            Inc(J);
          end;
          First;
          EnableControls;
        end;
        workbook.saveas(xelfilename);
        workbook.close;
        elapp.Quit;
        elapp := unassigned;
      except
        ShowMessage('您的机器里未安装Microsoft Excel');
        exit;
      end;
      ShowMessage(MSG_SAVE_DOON)
    end;
     
      

  3.   

    如果可以用 DbGridEh :    uses DBGridEhImpExp ;    SaveDBGridEhToExportFile(TDBGridEhExportAsXLS, DBGridEh1, FileNameStr, True);
      

  4.   

    下面给出用到的方法:
      //注意:下面的方法必须包含 ComObj, Excel97 单元
      //----------------------------------------------------------- 
      // if toExcel = false, export dbgrid contents to the Clipboard 
      // if toExcel = true, export dbgrid to Microsoft Excel 
      procedure ExportDBGrid(toExcel: Boolean); 
      var 
        bm: TBook; 
        col, row: Integer; 
        sline: String; 
        mem: TMemo; 
        ExcelApp: Variant; 
      begin 
        Screen.Cursor := crHourglass; 
        DBGrid1.DataSource.DataSet.DisableControls; 
        bm := DBGrid1.DataSource.DataSet.GetBook; 
        DBGrid1.DataSource.DataSet.First; 
      
        // create the Excel object 
        if toExcel then 
        begin 
          ExcelApp := CreateOleObject('Excel.Application'); 
          ExcelApp.WorkBooks.Add(xlWBatWorkSheet); 
          ExcelApp.WorkBooks[1].WorkSheets[1].Name := 'Grid Data'; 
        end; 
      
        // First we send the data to a memo 
        // works faster than doing it directly to Excel 
        mem := TMemo.Create(Self); 
        mem.Visible := false; 
        mem.Parent := MainForm; 
        mem.Clear; 
        sline := ''; 
      
        // add the info for the column names 
        for col := 0 to DBGrid1.FieldCount-1 do 
          sline := sline + DBGrid1.Fields[col].DisplayLabel + #9; 
        mem.Lines.Add(sline); 
      
        // get the data into the memo 
        for row := 0 to DBGrid1.DataSource.DataSet.RecordCount-1 do 
        begin 
          sline := ''; 
          for col := 0 to DBGrid1.FieldCount-1 do 
            sline := sline + DBGrid1.Fields[col].AsString + #9; 
          mem.Lines.Add(sline); 
          DBGrid1.DataSource.DataSet.Next; 
        end; 
      
        // we copy the data to the clipboard 
        mem.SelectAll; 
        mem.CopyToClipboard; 
      
        // if needed, send it to Excel 
        // if not, we already have it in the clipboard 
        if toExcel then 
        begin 
          ExcelApp.Workbooks[1].WorkSheets['Grid Data'].Paste; 
          ExcelApp.Visible := true; 
        end; 
      
        FreeAndNil(mem); 
      //  FreeAndNil(ExcelApp); 
        DBGrid1.DataSource.DataSet.GotoBook(bm); 
        DBGrid1.DataSource.DataSet.FreeBook(bm); 
        DBGrid1.DataSource.DataSet.EnableControls; 
        Screen.Cursor := crDefault; 
      end; 
      

  5.   

    unit ExcelTest;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls, Buttons, ExtCtrls,db,DBTables, ComCtrls;
    type
      TForm1 = class(TForm)
        Button1: TButton;
        Query1: TQuery;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure WriteDatasetToExcel(AQueryName: TQuery; AStrVar: String);
      end;var
      Form1: TForm1;implementationuses Comobj;{$R *.DFM}{ TForm1 }procedure TForm1.WriteDatasetToExcel(AQueryName: TQuery; AStrVar: String);
    var
      EclApp,WorkBook : Variant;
      xlsFileName : String ;
      I : Integer ;
      column : Integer ;
      Row : Integer ;
      Fdate:TDateTime;
      Year, Month, Day, Hour, Min, Sec, MSec: Word;
      StrDate:String ;
      StrDate1:String ;
    Begin
      Fdate:=now ;
      DecodeDate(Fdate, Year, Month, Day);
      DecodeTime(Fdate, Hour, Min, Sec, MSec);
      StrDate:=formatdatetime('yyyy-mm-dd-hh-mm-ss',Fdate) ;
      StrDate1:=formatdatetime('yyyy/mm/dd hh:mm:ss',Fdate) ;
     If AStrVar='Excel文件测试' Then
        Begin
          xlsfilename :='Excel文件测试' ;
        End ;  Try
        Begin
          EclApp := CreateOleObject('Excel.Application');
          WorkBook:=CreateOleObject('Excel.Sheet');
        End
      Except
        ShowMessage('您的计算机上没有 Microsoft Excel!');
        Exit;
      end;
      try
        workBook:=EclApp.workBooks.Add ;
        row:=2;
        EclApp.Workbooks.Item[1].Activate;
        eclApp.Cells.font.colorindex:=5 ;
        EclApp.Activesheet.Cells(1,1):=AStrVar ;
        For I := 1 To AQueryName.FieldCount Do
            EclApp.Activesheet.Cells(2,I):=AQueryName.Fields[I-1].FieldName ;
        If Not AQueryName.Active Then AQueryName.Active := True ;
        AQueryName.First ;
      While Not(AQueryName.Eof) do
      begin
        column:=1;
        for i:=1 to AQueryName.FieldCount do
        begin
          eclApp.Cells.Item[row+1,column]:=AQueryName.fields[i-1].AsString;
          column:=column+1;
        end;
        AQueryName.Next;
        row:=row+1;
      End ;
        WorkBook.saveas(xlsFileName);
        WorkBook.close;
        WorkBook:=eclApp.workBooks.Open(xlsFileName);
      if MessageDlg('xlsFileName'+'对该文件是否保存?',
            mtConfirmation,[mbYes, mbNo], 0) = mrYes then
        WorkBook.save
      Else
        workBook.Saved := True;
        WorkBook.Close;
        eclApp.Quit;
        eclApp:=Unassigned;
      except
          ShowMessage('Excel 文件保存失败');
          WorkBook.close;
          eclApp.Quit;  {释放VARIANT变量}
          eclApp:=Unassigned;
      end;
      ShowMessage('EXCEL 文件保存完毕') ;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      WriteDatasetToExcel(query1,'Excel文件测试');
    end;end.