将DBGrid的数据导出到Excel文件保存

本文介绍了如何在Delphi项目中将DBGrid中的数据导出到Excel文件,并进行了功能优化,包括进度提示、用户自定义每页数据量、表名设置和导出过程中的取消操作。
摘要由CSDN通过智能技术生成

近来一段时间忙的慌,接了个帮人升级系统的小单子。其中涉及到将DbGrid的数据转到Excel文件并保存的功能,其实本身倒也不难。只是有些麻烦。想想这种功能,肯定有先人已经写好的现成东西直接拿过来用就应该OK了。于是Google一番,果然有很多类似的例子代码,俺在盒子上找到了和俺的需求相近的一个代码DbGrid2Excel这个代码。他那个写的确实不错,但是有些地方也还是不能尽如人意的哈,于是就在他的代码上修改了下,同时新增加了进度提示的窗口,导出时能随时取消的功能。分页方面不再固定死了,而是由用户规定一个表中最多能存放多少条数据。同时增加表名称的设置。呵呵,废话也不多说,直接贴代码吧

DelphiCode:
(*
  原作者: iamdream(delphi盒子)
  修改: 不得闲
  功能: 将DbGrid数据保存到Excel
  参数:
      Grid指定表格
      FileName指定要保存的文件名
      MaxPageRowCount指定一页最多的支持行数
     ShowProgress 指定是否显示进度条
  用法:
   SaveDbGridAsExcel(DBGrid1,'C:/2.xls','表测试',2000);
*)

procedure SaveDbGridAsExcel(Grid: TDBGrid;const FileName,title: string;
const MaxPageRowCount: Integer = 65535;const ShowProgress: Boolean = True);
const               
  MAX_VAR_ONCE   = 1000;     //一次导出的条数
var                          //返回导出记录条数
  Excel, varCells: Variant;
  MySheet, MyCells, Cell1, Cell2, Range: OleVariant;
  iRow, iCol, iSheetIdx, iVarCount, iCurRow: integer;
  CurPos: TBookmark;
  ProgressForm: TForm;
  Prompt: TLabel;
  progressBar:  TProgressBar;
  Panel : TPanel;
  Button : TButton;
  procedure ReSetObjEvent(OldEventAddr: pointer;NewEventValue: pointer;ReSetObject: TObject);
  begin
     TMethod(OldEventAddr^).Code := NewEventValue;
     TMethod(OldEventAddr^).Data := ReSetObject;
  end;

  procedure ButtonClick(BtnObject: TObject;Sender: TObject);
  begin
    TComponent(BtnObject).Tag := Integer(MessageBox(Application.Handle,
                                         '真的要终止数据的导出吗?','确认',
                                         MB_OKCANCEL + MB_ICONIN
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
void __fastcall TPhoneForm::SelectButtonClick(TObject *Sender) {   AnsiString StrDate, ExName;//存放日期用于sheet   AnsiString Datatem,phone1="拨号";//临时存放数据库的字段值   int i,j;   //查询所需的数据   PhoneADOQuery->Close();   PhoneADOQuery->Parameters->ParamByName("date1")->Value=PhoneMaskEdit1->Text;   PhoneADOQuery->Parameters->ParamByName("date2")->Value=PhoneMaskEdit2->Text;   PhoneADOQuery->Active=true;   //新建一个EXCEL   Ex = Variant::CreateObject("Excel.Application");   Ex.OlePropertyGet("workbooks").OleFunction("Add", 6);   Wb = Ex.OlePropertyGet("ActiveWorkBook");   Sh = Wb.OlePropertyGet("ActiveSheet");   Ex.OlePropertySet("Visible", true);   //给sheet以日期重命名,   StrDate=DateToStr(Date());   Sh.OlePropertySet("Name", StrDate.c_str());   //给EXCEL输入数据   for (j=0;j<PhoneADOQuery->FieldCount;j++)   {      Datatem=PhoneADOQuery->Fields->Fields[j]->FieldName;      Sh.OlePropertyGet("Cells", 1, j+1).OlePropertySet("Value", Datatem.c_str());   }   PhoneADOQuery->First();   for (i=0; i<PhoneADOQuery->RecordCount; i++)   {   for (j=0;j<PhoneADOQuery->FieldCount;j++)   {      Datatem=PhoneADOQuery->Fields->Fields[j]->AsString;      Sh.OlePropertyGet("Cells", i+2, j+1).OlePropertySet("Value", Datatem.c_str());      if (phone1==PhoneADOQuery->Fields->Fields[j]->FieldName)     {Sh.OlePropertyGet("Cells", i+2, j+1).OlePropertySet("NumberFormatLocal", "0_ ");//设置单元格格式为数值格式 }   }   PhoneADOQuery->Next();   }   //保存EXCEL并退出   ExName=GetCurrentDir()+"\\"+DateToStr(Date())+".xls";   Wb.OleFunction("SaveAs", ExName.c_str());   Wb.OleFunction("Close");   Ex.OleFunction ("Quit");   Ex = Unassigned; }
发布原因:在网上找了很久也没找到个好用的,不是慢,就是要依赖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;
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值