请问怎样将数据表中的内容显示在excel中?急
最好能给个例子,谢了
[email protected]

解决方案 »

  1.   

    procedure TfrmGlobal.DBGridInFoToExcel(FileName, TitleCaption: string;
      MakeDataSource: TDataSource; makeDBGrid: TDBGrid);
    var
         xlApp, xlSheet, szValue: Variant;
         ARow, iLoop: word;
    begin
         xlApp := CreateOleObject('Excel.Application');
         try
             xlSheet := CreateOleObject('Excel.Sheet');
             xlSheet := xlApp.WorkBooks.Add;
             
             // 主标题
              xlSheet.WorkSheets[1].Cells[1,1] := TitleCaption;
             
        //  表格标题     
             for iLoop := 0 to makeDBGridEh.Columns.Count - 1 do
                  xlSheet.WorkSheets[1].Cells[2, iLoop+1] := makeDBGridEh.Columns[iLoop].Title.Caption;         // 数据
             ARow := 3;
             with MakeDataSource.DataSet do
             begin
                  DisableControls;
                  First;
                  while not Eof do
                  begin
                       for iLoop := 0 to Fields.Count - 1 do
                       begin
                           szValue := Fields[iLoop].Value;
                           xlSheet.WorkSheets[1].Cells[ARow, iLoop+1] := szValue;
                       end;
                       Inc(ARow);
                       Next;
                  end;
                  First;
                  EnableControls;
             end;         try
                  xlSheet.SaveAs(FileName);
                  Application.MessageBox('导出完毕!', '提示', MB_IconExclamation);
             finally
                  xlSheet.Close;
                  xlApp.Quit;
                  xlApp := UnAssigned;
             end;
         except
              MessageBox(handle, '本机没有安装Excel.', '提示',MB_IconExclamation);
         end;end;
    ...
    uses Excel2000, {C:\Program Files\Borland\Delphi6\Imports}OleServer;procedure TFrmMain.WriteExcel(AdsData: TADODataSet; sName, Title: string);
    var
    ExcelApplication1: TExcelApplication;
    ExcelWorksheet1: TExcelWorksheet;
    ExcelWorkbook1: TExcelWorkbook; 
    i, j: integer; 
    filename: string;
    begin 
    filename := concat(extractfilepath(application.exename), sName, '.xls');
    try
    ExcelApplication1 := TExcelApplication.Create(Application); 
    ExcelWorksheet1 := TExcelWorksheet.Create(Application); 
    ExcelWorkbook1 := TExcelWorkbook.Create(Application); 
    ExcelApplication1.Connect; 
    except 
    Application.Messagebox('Excel 没有安装!', 'Hello', MB_ICONERROR + mb_Ok);
    Abort; 
    end; 
    try 
    ExcelApplication1.Workbooks.Add(EmptyParam, 0); 
    ExcelWorkbook1.ConnectTo(ExcelApplication1.Workbooks[1]); 
    ExcelWorksheet1.ConnectTo(ExcelWorkbook1.Worksheets[1] as _worksheet); 
    AdsData.First; 
    for j := 0 to AdsData.Fields.Count - 1 do 
    begin 
    ExcelWorksheet1.Cells.item[3, j + 1] := AdsData.Fields[j].DisplayLabel; 
    ExcelWorksheet1.Cells.item[3, j + 1].font.size := '10'; 
    end; 
    for i := 4 to AdsData.RecordCount + 3 do 
    begin 
    for j := 0 to AdsData.Fields.Count - 1 do 
    begin 
    ExcelWorksheet1.Cells.item[i, j + 1] :=AdsData.Fields[j].Asstring;
    ExcelWorksheet1.Cells.item[i, j + 1].font.size := '10';
    end; 
    AdsData.Next; 
    end;
    ExcelWorksheet1.Columns.AutoFit;
    ExcelWorksheet1.Cells.item[1, 2] := Title;
    ExcelWorksheet1.Cells.Item[1, 2].font.size :='14'; 
    ExcelWorksheet1.SaveAs(filename); 
    Application.Messagebox(pchar('数据成功导出' + filename), 'Hello',mb_Ok);
    finally 
    ExcelApplication1.Disconnect; 
    ExcelApplication1.Quit; 
    ExcelApplication1.Free; 
    ExcelWorksheet1.Free; 
    ExcelWorkbook1.Free; 
    end; 
    end;procedure Tfrmmain.FormCreate(Sender: TObject);
    begin
    WriteExcel(ADODataSet1, 'ergonge','hhh');
    end;