var
  i:integer;
  str:String;
  StrList:TStringList;//用于存储数据的字符列表
begin
  try
    with Table1 do//可以是TABLE,也可以是QUERY等等
    begin
      First;//指针至表头
      DisableControls;
      while not EOF do
      begin
        str:=';
        for i:=0 to FieldCount-1 do
        begin
          str:=str+Fields[i].AsString+#19;
        StrList.Add(str);
        Next;
      end;
      StrList.SaveToFile('c:\a.xls');//存储路径
      EnableControls;
    end
  finally
    StrList.free;//释放内存
  end;
end;

解决方案 »

  1.   

    写入excel的程序我有,要的就发给你吧
      

  2.   

    联接SQLSERVER,显示数据内容到DBGrid里。
    以下是将DBGrid内容导入EXECL:
    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; 
      

  3.   

    最简单的方法,使用第三方的vcl如:EMS Quick Export component。
    可以导出多种格式文件,word,excel,htm...
      

  4.   

    采用数据导出方式:
    给你一个例子 
    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;