请问如何实现数据表中的记录输出为Excel文件,急用,谢谢!

解决方案 »

  1.   

    有一个控件的。好像是ExGrid中的吧,有一个控件可以的。上网找一找看,我忘了!!!
      

  2.   

    这是我自己一个控件中的源代码,你可以看看,主要是通过ole控制
    procedure Tmydbgrid.chooseexcel(sender: tobject);
    var
      msexcel, sheet: OleVariant;
      count, i: integer; //fieldnum,
      Grid: twwdbgrid;
    begin
      grid := self;
      if grid.DataSource.DataSet.Active = false then
      begin
        prompt(mtinformation, '数据为空!');
        exit;
      end;
      try
        msexcel := CreateOleObject('excel.Application');
        msexcel.visible := true;
      except
        prompt(mtinformation, '不能启动Microsoft Excel,操作失败!');
        exit;
      end;
      count := 2;
      msexcel.Workbooks.Add(emptyparam);
      msexcel.Workbooks[1].WorkSheets[1].Name := 'Excel';
      sheet := msexcel.Workbooks[1].WorkSheets['Excel'];
      for i := 1 to grid.FieldCount do
      begin
        if grid.UseTFields then
          sheet.cells[1, i] := grid.datasource.dataset.fields[i - 1].DisplayLabel
        else
          sheet.cells[1, i] := grid.Columns[i - 1].DisplayLabel;
      end;
      try
        grid.DataSource.DataSet.DisableControls;
        grid.DataSource.DataSet.First;
        while not grid.DataSource.DataSet.Eof do
        begin
          for i := 1 to grid.FieldCount do
            sheet.cells[count, i] := grid.Fields[i - 1].asstring;
          inc(count);
          grid.DataSource.DataSet.Next;
        end;
        grid.DataSource.DataSet.First;
      finally
        grid.DataSource.DataSet.EnableControls;
      end;
    end;
      

  3.   

    有可以实现合并单元格或是其他一系列Execl常规操作的吗?不然的话,也可以不用ole来做啊。
      

  4.   

    try
          assignfile(F, SaveDialog1.FileName);
          if FileExists(SaveDialog1.FileName) then
            if application.MessageBox('文件已存在,是否重写?', '提示', MB_YESNO) = IDYES then
              rewrite(F)
            else
              exit
          else
            rewrite(F);
          //取标题 
          with DBGrid1 do
            for i := 0 to Columns.Count - 1 do
              if DBGrid1.Columns[i].Visible then
                sLabel := sLabel + Columns[i].title.Caption+#9;
          //写文件
          write(F, #9#9);
          write(F, sTitle + #13#10);
          write(F, sLabel + #13#10);
          // 取数据
          with DBGrid1.DataSource.DataSet do begin
            SavePlace := GetBook;
            DisableControls;
            try
              First;
              while not EOF do begin
                { Process each record here }
                sData := '';
                for i := 0 to DBGrid1.Columns.Count - 1 do begin
                  //如列不可见,不取 
                  if DBGrid1.Columns[i].Visible then begin
                    if option=1 then
                      sData := sData + TField(DBGrid1.Columns[i].field).text+#9
                    else
                      sData := sData + TField(DBGrid1.Columns[i].field).AsString+#9;
                  end;
                end;
                   //写文件
                write(F, sData + #13#10);
                Next;
              end;
            finally
              EnableControls;
            end;
            GotoBook(SavePlace);
            FreeBook(SavePlace);
          end; //end of with
        except
          on EInOutError do
            application.MessageBox('写文件错误', '错误提示', MB_OK);
        end;
        closefile(F);
      

  5.   

    同意:SuQingQuan(边城浪子) 还有一个方法是先存成CSV(逗号分隔文本文件)这样也可以用Excel打开,不过后缀一定要是
    CSV,如果非要存成XLS的话那就再转一下吧(CSV->XLS),给个我写的Function你.function SaveCSV2XLS(const FileName: string): string;
    var
        app: OleVariant;
        xlsFile: string;
    begin
        xlsFile := ChangeFileExt(FileName, '.xls');
        app := CreateOleObject('Excel.Application');
        if FileExists(xlsFile) then SysUtils.DeleteFile(xlsFile);    app.Workbooks.Open(FileName);
        app.Workbooks[1].SaveAs(xlsFile, -4143, '', '', false, false);
        app.Workbooks[1].Close;
        app := Unassigned;
        SysUtils.DeleteFile(FileName);
        result := ChangeFileExt(FileName, '.xls');
    end;