请问要怎么样才能把dbgrid中的数据导入到excel表中呢?
谢谢各位,能帮我一下吗?

解决方案 »

  1.   

    建议用TMS的advStringGrid
    你可以读取单元格的值,然后给adoTable的值
      

  2.   

    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); 
       sheet:=excelapp.workbooks[1].worksheets[1];
       sheet.name:='sheet1';
       sheet.paste;
       Clipboard.Clear;
       excelapp.visible:=true;end;//---Code From CSDN
      

  3.   

    保存为ExcelButton.pas,然后安装这个控件即可,我用了四年了.
    unit ExcelButton;
    {$R TExcelButton.dcr}interfaceuses
      Windows, Messages, SysUtils, Classes, Controls, StdCtrls, Buttons,
      DB, OleServer, Excel2000;type
      TExcelButton = class(TBitBtn)
      private
        fSheetName,fFilePath : string;
        fDataSource : TDataSource;
      protected
        procedure SetDataSource(Value : TDataSource);
        procedure ExportToExcel(SheetName : string;FilePath : string);
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
          X, Y: Integer); override;
      public
        { Public declarations }
        procedure ExportFile;
      published
        property SheetName : string read fSheetName write fSheetName;
        property FilePath : string read fFilePath write fFilePath;
        property DataSource : TDataSource read fDataSource write SetDataSource;
      end;procedure Register;implementationprocedure Register;
    begin
      RegisterComponents('Additional', [TExcelButton]);
    end;procedure TExcelButton.SetDataSource(Value : TDataSource);
    begin
          fDataSource := Value;
    end;procedure TExcelButton.ExportFile;
    begin
          ExportToExcel(fSheetName,fFilePath);
    end;procedure TExcelButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
          X, Y: Integer);
    begin
          inherited;
          ExportToExcel(fSheetName,fFilePath);
    end;procedure TExcelButton.ExportToExcel(SheetName : string; FilePath : string);
    var
         col, row: Integer;
         WorkBk : _WorkBook; //Define a WorkBook
         WorkSheet : _WorkSheet; //Define a WorkSheet
         IIndex : OleVariant;
         ExcelApp : TExcelApplication;
    begin
       if (fDataSource.DataSet.Active) AND
            (SheetName <> '') AND
            (FilePath <> '') then
       begin    IIndex := 1;
        Try
         ExcelApp := TExcelApplication.Create(Self);     // create the Excel object
         ExcelApp.Connect;
         ExcelApp.WorkBooks.Add(xlWBatWorkSheet,0);     // Select the first WorkBook
         WorkBk := ExcelApp.WorkBooks.Item[IIndex];     // Define the first WorkSheet
         WorkSheet := WorkBk.WorkSheets.Get_Item(1) as _WorkSheet;     //WorkSheet.Name := SheetName;
         Worksheet.Columns.HorizontalAlignment := xlLeft;
         WorkSheet.Columns.ColumnWidth := 14;
         WorkSheet.Columns.RowHeight := 16;
         // add the info for the column names
         for col := 1 to fDataSource.DataSet.FieldCount do
             WorkSheet.Cells.Item[1,col] := fDataSource.DataSet.Fields[col-1].DisplayLabel;     // get the data into the memo
         with fDataSource do
         begin
         DataSet.First;
         row := 2;
         while not DataSet.Eof do
         begin
               for col := 1 to DataSet.FieldCount do
               begin
                       WorkSheet.Cells.Item[row,col] := DataSet.Fields[col-1].AsString;
               end;
               INC(row,1);
               DataSet.Next;
         end;
         end;          ExcelApp.DisplayAlerts[0] := False;
              WorkBk.SaveCopyAs(FilePath,0);
              ExcelApp.Workbooks.Close(0);
              ExcelApp.Quit;
        Finally
              ExcelApp.Free;
        end;
       end;
    end;end.
      

  4.   

    放一个MEMO和SaveDialog 控件。
    procedure  TForm1.Button1Click(Sender: TObject);
    var
         ls_str,ls_tab :string;
         i,j : integer;
    begin
         SaveDialog1.FileName:='文件名';
         if  SaveDialog1.Execute then
         begin
         ls_tab:=#9;
         dbgrid1.DataSource.DataSet.First;
         memo1.clear;
         memo1.Lines.add(文件名);
         for i:=0 to dbgrid1.Columns.Count-1 do
            begin
                 ls_str:= ls_str+dbgrid1.Columns[i].Title.Caption+ls_tab;
            end;
            memo1.Lines.add(ls_str);
         while not dbgrid1.DataSource.DataSet.Eof do
         begin
            ls_str:='';
            for i:=0 to dbgrid1.Columns.Count-1 do
            begin
                 ls_str:= ls_str+dbgrid1.Columns[i].field.asstring+ls_tab;
            end;
            memo1.Lines.add(ls_str);
            dbgrid1.DataSource.DataSet.next;
         end;
           memo1.Lines.SaveToFile(SaveDialog1.FileName+'.XLS');
         end;
         application.MessageBox('导出成功,请用EXCEL打开文件!','提示',64);end;