如何将DBGrid中的数据原样导出到Excel表中

这段代码展示了如何使用Delphi创建一个库,该库包含两个函数sendDBGridToExcel和sendDBGridToExcel_F,用于将TDBGrid组件中的数据原样导出到Excel工作表中。在导出过程中,它会打开数据源,检查是否有数据,然后创建一个新的Excel工作簿,设置单元格内容,并将数据逐行写入Excel表格。
摘要由CSDN通过智能技术生成

library LoadDBGridToExcel;


uses
  ShareMem,
  SysUtils,
  Classes,  Windows,Variants,  Dialogs, DBGrids, ExcelXP,Graphics;

{$R *.res}
function sendDBGridToExcel_F(dbg :TDBGrid;sheetName :string;show: Boolean):TDateTime;stdcall;
var
  i,row,column,icount:integer;
  excelApplication:TexcelApplication;
  excelWorkBook: TExcelWorkbook;
  excelWorkSheet: TExcelWorksheet;
begin
  dbg.DataSource.DataSet.Open;

  if (dbg.DataSource.DataSet.RecordCount < 1) or ( not dbg.DataSource.DataSet.Active) then
  begin
    Result := 0;
    messagebox(getactivewindow(),'无数据!','提示',MB_OK+MB_ICONINFORMATION);
    exit;
  end;

  try
    excelApplication := TExcelApplication.Create(nil);
    excelWorkBook := TExcelWorkbook.Create(nil);
    excelWorkSheet := TExcelWorksheet.Create(nil);
    excelApplication.Connect;
    excelApplication.Visible[0] := show; {显示过程}
    excelApplication.Caption := 'Excel Application';
    excelApplication.Workbooks.Add(null,0);
    excelWorkBook.ConnectTo(excelApplication.Workbooks[1]);
    excelWorkSheet.ConnectTo(excelWorkBook.Worksheets[1] as _Worksheet);
    excelWorkSheet.Name := sheetName;


    row := 2;
    dbg.DataSource.DataSet.First;
    for icount := 0 to dbg.Columns.Count - 1 do
    begin
      excelWorkSheet.Cells.Item[1,icount + 1] := dbg.Columns.Items[icount].Title.Caption;
      excelWorkSheet.Cells.Item[1,icount + 1].Font.color := clred;
      excelWorkSheet.Cells.Item[1,icount + 1].Font.Name := '黑体';
    end;
    while not (dbg.DataSource.DataSet.Eof) do
    begin
      column := 1;
      for i := 0 to dbg.Columns.Count - 1 do
      begin
        excelWorkSheet.Cells.Item[row,column] := ''''+dbg.DataSource.DataSet.fieldByName(dbg.Columns.Items[i].FieldName).AsString;
        column := column + 1;
      end;
      dbg.DataSource.DataSet.Next;
      row := row + 1;
    end;
    excelApplication.Visible[0] := True;
    excelApplication.Connect;
    excelApplication.Free;
    excelWorkSheet.Free;
    excelWorkBook.Free;
    Result := Now;
    messagebox(getactivewindow(),'数据导出EXCEL成功!','提示',MB_OK+MB_ICONINFORMATION);
  except
    Result := 0;
    MessageDlg('Excel可能尚未安装!',mtError,[mbOK],0);
    Abort;
  end;
end;

procedure sendDBGridToExcel(dbg :TDBGrid;sheetName :string);stdcall;
var
  i,row,column,icount:integer;
  excelApplication:TexcelApplication;
  excelWorkBook: TExcelWorkbook;
  excelWorkSheet: TExcelWorksheet;
begin
  dbg.DataSource.DataSet.Open;

  if (dbg.DataSource.DataSet.RecordCount < 1) or ( not dbg.DataSource.DataSet.Active) then
  begin
    messagebox(getactivewindow(),'无数据!','提示',MB_OK+MB_ICONINFORMATION);
    exit;
  end;

  try
    excelApplication := TExcelApplication.Create(nil);
    excelWorkBook := TExcelWorkbook.Create(nil);
    excelWorkSheet := TExcelWorksheet.Create(nil);
    excelApplication.Connect;
    excelApplication.Visible[0] := True; {显示过程}
    excelApplication.Caption := 'Excel Application';
    excelApplication.Workbooks.Add(null,0);
    excelWorkBook.ConnectTo(excelApplication.Workbooks[1]);
    excelWorkSheet.ConnectTo(excelWorkBook.Worksheets[1] as _Worksheet);
    excelWorkSheet.Name := sheetName;


    row := 2;
    dbg.DataSource.DataSet.First;
    for icount := 0 to dbg.Columns.Count - 1 do
    begin
      excelWorkSheet.Cells.Item[1,icount + 1] := dbg.Columns.Items[icount].Title.Caption;
      excelWorkSheet.Cells.Item[1,icount + 1].Font.color := clred;
      excelWorkSheet.Cells.Item[1,icount + 1].Font.Name := '黑体';
    end;
    while not (dbg.DataSource.DataSet.Eof) do
    begin
      column := 1;
      for i := 0 to dbg.Columns.Count - 1 do
      begin
        excelWorkSheet.Cells.Item[row,column] := dbg.DataSource.DataSet.fieldByName(dbg.Columns.Items[i].FieldName).AsString;
        column := column + 1;
      end;
      dbg.DataSource.DataSet.Next;
      row := row + 1;
    end;
    excelApplication.Visible[0] := True;
    excelApplication.Connect;
    excelApplication.Free;
    excelWorkSheet.Free;
    excelWorkBook.Free;
    messagebox(getactivewindow(),'数据导入EXCEL成功!','提示',MB_OK+MB_ICONINFORMATION);
  except
    MessageDlg('Excel尚未安装!',mtError,[mbOK],0);
    Abort;
  end;
end;

exports
  sendDBGridToExcel,sendDBGridToExcel_F;

begin
end.

发布原因:在网上找了很久也没找到个好用的,不是慢,就是要依赖Excel(必须按装),又找呀找,才找到一个http://blog.csdn.net/xiangding/archive/2003/10/27/16918.aspx,但试了一下,不能用,不知是我用法不对还是本身有问题(由于没有给出使用示例),后来找到http://developer.51cto.com/art/200510/7494.htm这个不错,但他又让我装ehlid,不爽,所以对其做了修改,其它也就是去了个加页脚的地方,很爽,有进度条,导完后如果按安装了Excel则直接打开,如果没有的话就不管了,自已想办法打开吧!特点:一、不需安装Excel即可导出。二、有进度条。三、最多可以导出多少条,我没试,我试了5万条没有问题,时间也就是只需1-5分钟(我电脑较慢)。四、不需安装组件。使用方法(注:别忘了uses DBGridToExcel;):procedure TForm1.btn1Click(Sender: TObject);var DBGridToExcel: TDBGridToExcel; ExcelFileName: string;begin DBGridToExcel := TDBGridToExcel.Create(nil); try DBGridToExcel.TitleName := '入网车辆统计报表'; DBGridToExcel.BeginDate := '开始日期:2005-07-01'; DBGridToExcel.EndDate := '结束日期:2005-07-18'; DBGridToExcel.UserName := '系统管理员'; DBGridToExcel.DBGrid := dbgrd1; DBGridToExcel.ShowProgress := True; if ExcelFileName = '' then begin ExcelFileName := 'c:\1.xls'; with TSaveDialog.Create(nil) do begin Filter := 'Microsoft Excel xls文件|*.xls'; DefaultExt := 'xls'; if not Execute then Exit else ExcelFileName := FileName; end; end; DBGridToExcel.FileName := ExcelFileName; DBGridToExcel.Open := False; DBGridToExcel.ExportToExcel; finally DBGridToExcel.Free; end;end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值