捐赠 | 广告 | 注册 | 发布 | 上传 | 关于我们    
  沪ICP备05001939号 DELPHI盒子 | 盒子论坛 | 盒子文章 | 盒子问答悬赏 | 最新更新 | 论坛检索 | 下载中心 | 高级搜索    
  精品专区 | 繁體中文 | 奖励公告栏 | 直通车账号登陆 | 关闭GOOGLE广告 | 临时留言    
盒子资源分类
全部展开 - 全部合拢
DBGrid分页(Sheet)导出到Excel (支持超过65536条记录)
关键字:DBGrid Excel 导出 分页 Sheet 超大记录 数据库 DBGridToExcel
来 自:原创
平 台:Win9x,Win2k/XP/NT,Win2003 下载所需:0 火柴
深浅度:初级 完成时间:2005/9/2
发布者:iamdream 发布时间:2005/9/2
编辑器:DELPHI7 语  种:简体中文
分 类:数据库 下载浏览:5752/29851
加入到我的收藏
下载错误报错
登陆以后才能下载
 用户名:
 密 码:
自动登陆(30天有效)
图片如果打不开,说明流量不够了,请稍候下载……
    DBGrid2Excel.pas文件中是一个函数,只需将该文件加到你的工程里,引用一下,然后直接调用DBGridToExcel函数即可。
    这个DBGridToExcel函数支持几乎无限的记录,它会自动分页(Sheet),所以这下导出时不用担心记录太多,速度也比较快。
    至于用处大不大,我也不知道,反正到目前为止,我还未在实际的工作中用过,只是自己好玩写出来的。
    祝你愉快!
Google
 
本站原创作品,未经作者许可,严禁任何方式转载;转载作品,如果侵犯了您的权益,请联系我们
龙脉加密锁 15元起 Grid++Report 报表 申请支付@网
 相关文章
TscExcelExport v3.6 FS
cqwty 2007/12/2 下+4654/浏+14613 评+1
将数据导出到Excel的类Export2Excel
lanyaoshi 2007/8/8 下+4682/浏+17598 评+1
DBGrid2Excel 修改版
galfordliu 2006/12/30 下+3870/浏+20037 评+11
Interbase 数据导出 Excel 工具
96sd2 2006/8/10 下+1255/浏+12677 评+0
将DBGrid/DBGridEh/DataSet中的数据导入到E…
mastersky 2005/12/10 下+6817/浏+24277 评+15
将DBGrid/DBGridEh/DataSet中的数据导入到E…
mastersky 2005/11/24 下+2317/浏+15525 评+6
DBGrid分页(Sheet)导出到Excel (支持超过65…
iamdream 2005/9/2 下+5752/浏+29852 评+57
导出DataSet到Excel的组件
pchaos 2005/9/1 下+2976/浏+17311 评+13
TscExcelExport v3.12 For D5-7 With FullS…
test004 2004/4/26 下+1644/浏+15218 评+5
相关评论
共有评论57条 当前显示最后6条评论
wgxis 2009/4/27 20:28:05
不知道为什么,导出时会显示错误(编译没错误)
Access violation at address 0047D2B1 in module 'warehouse.exe'.Read of address 000000D2
warehouse.exe 是我的项目
onionzq 2009/5/1 23:44:35
速度确实挺快的,Mark.
hongss 2009/5/7 12:07:36
  MyExcel.Visible := True;
  MyExcel.WorkBooks[1].Saved := True;
改为
  MyExcel.Visible := False;
  MyExcel.SaveAs('文件路径名');
Excel还是会打开,而且用SaveAs也存不下来。
用MyExcel.WorkBooks[1].SaveAs也不行……
suiyunonghen 2009/5/22 0:01:08
修改了一下,我也。

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_ICONINFORMATION) = IDOK);
  end;

  procedure CreateProgressForm;
  begin
    ProgressForm := TForm.Create(nil);
    With ProgressForm do
    begin
      Font.Name := '宋体';
      Font.Size := 10;
      BorderStyle := bsNone;
      Width := 280;
      Height := 120;
      BorderWidth := 1;
      Color := clBackground;
      Position := poOwnerFormCenter;
    end;
    Panel := TPanel.Create(ProgressForm);
    with Panel do { Create Panel }
    begin
      Parent := ProgressForm;
      Align := alClient;
      BevelInner := bvNone;
      BevelOuter := bvNone;
      Caption := '';
    end;

    Prompt := TLabel.Create(Panel);
    with Prompt do { Create Label }
    begin
      Parent := Panel;
      Left := 20;
      Top := 25;
      Caption := '正在启动Excel,请稍候……';
    end;

    progressBar := TProgressBar.Create(panel);
    with ProgressBar do { Create ProgressBar }
    begin
      Step := 1;
      Parent := Panel;
      Smooth := true;
      Left := 20;
      Top := 50;
      Height := 18;
      Width := 260;
    end;

    Button := TButton.Create(Panel);
    with Button do { Create Cancel Button }
    begin
      Parent := Panel;
      Left := 115;
      Top := 80;
      Caption := '关闭';
    end;
    ReSetObjEvent(@@Button.OnClick,@ButtonClick,Button);
    ProgressForm.FormStyle := fsStayOnTop;
    ProgressForm.Show;
    ProgressForm.Update;
  end;

begin
  if (Grid.DataSource <> nil) and
     (Grid.DataSource.DataSet <> nil) and
     Grid.DataSource.DataSet.Active then
  begin
    Grid.DataSource.DataSet.DisableControls;
    CurPos  := Grid.DataSource.DataSet.GetBookmark;
    Grid.DataSource.DataSet.First;
    try
      if ShowProgress then
      begin
        CreateProgressForm;
        Button.Tag := 0; 
      end;
      Excel := CreateOleObject('Excel.Application');
      Excel.WorkBooks.Add;
      Excel.Visible := False;
    except
      Application.Messagebox('Excel 没有安装!','操作提示', MB_IConERROR + mb_Ok);
      Screen.Cursor := crDefault;
      Grid.DataSource.DataSet.GotoBookmark(CurPos);
      Grid.DataSource.DataSet.FreeBookmark(CurPos);
      Grid.DataSource.DataSet.EnableControls;
      if ProgressForm <> nil then
         ProgressForm.Free;
      exit;
    end;
    if Grid.DataSource.DataSet.RecordCount <= MAX_VAR_ONCE then
      iVarCount := Grid.DataSource.DataSet.RecordCount
    else iVarCount := MAX_VAR_ONCE;
    varCells  := VarArrayCreate([1, iVarCount,1,Grid.FieldCount],varVariant);

    iSheetIdx := 1;
    iRow      := 0;
    if ShowProgress then
    begin
      ProgressBar.Position := 0;
      Prompt.Caption := '请等待,正在导出数据……';
      ProgressBar.Max := Grid.DataSource.DataSet.RecordCount;
    end;
    while (not Grid.DataSource.DataSet.Eof and not ShowProgress) or
       (not Grid.DataSource.DataSet.Eof and ShowProgress and (Button.Tag = 0)) do
    begin
      if (iRow = 0) or (iRow > MaxPageRowCount + 1) then
      begin
        if iSheetIdx <= Excel.WorkBooks[1].WorkSheets.Count then
          MySheet := Excel.WorkBooks[1].WorkSheets[iSheetIdx]
        else
          MySheet := Excel.WorkBooks[1].WorkSheets.Add(NULL, MySheet);//加在后面
        MySheet.Name := Title + IntToStr(iSheetIdx);
        MyCells := MySheet.Cells;
        Inc(iSheetIdx);
        //开始新的数据表
        iRow := 1;
        //写入表头
        for iCol := 1 to Grid.FieldCount  do
        begin
          MySheet.Cells[1, iCol] := Grid.Columns[iCol-1].Title.Caption;
          MySheet.Cells[1, iCol].Font.Bold := True;
          if (Grid.Fields[iCol - 1].DataType = ftString) or 
          (Grid.Fields[iCol - 1].DataType = ftWideString) then
          //对于“字符串”型数据则设Excel单元格为“文本”型
          MySheet.Columns[iCol].NumberFormatLocal := '@';        
        end;        
        Inc(iRow);
      end;
      iCurRow := 1;
      while (not Grid.DataSource.DataSet.Eof and not ShowProgress) or
          (not Grid.DataSource.DataSet.Eof and ShowProgress and (Button.Tag = 0)) do
      begin
        for iCol := 1 to Grid.FieldCount do
        begin
          Application.ProcessMessages;
          if Grid.Fields[iCol - 1].IsBlob then
          varCells[iCurRow, iCol] := '二进制数据'
          else varCells[iCurRow, iCol] := Grid.Fields[iCol-1].AsString;
        end;
        Inc(iRow);
        Inc(iCurRow);
        if ShowProgress then
          ProgressBar.Position := ProgressBar.Position + 1;
        Application.ProcessMessages;
        Grid.DataSource.DataSet.Next;
        if (iCurRow > iVarCount) or (iRow > MaxPageRowCount + 1) then
        begin
          Application.ProcessMessages;
          Break;
        end;
      end;
      Cell1 := MyCells.Item[iRow - iCurRow + 1, 1];
      Cell2 := MyCells.Item[iRow - 1,Grid.FieldCount];
      Range := MySheet.Range[Cell1 ,Cell2];
      Range.Value := varCells;
      MySheet.Columns.AutoFit;
      Cell1    := Unassigned;
      Cell2    := Unassigned;
      Range    := Unassigned;
      Application.ProcessMessages;
    end;
    if (ShowProgress and (Button.Tag = 0)) or not ShowProgress then
      MySheet.saveas(FileName);
    MyCells  := Unassigned;
    varCells := Unassigned;
    Excel.WorkBooks[1].Saved := True;
    MySheet.application.quit;
    Excel.quit;
    Excel  := Unassigned;
    if CurPos <> nil then
    begin
      Grid.DataSource.DataSet.GotoBookmark(CurPos);
      Grid.DataSource.DataSet.FreeBookmark(CurPos);
    end;
    Grid.DataSource.DataSet.EnableControls;
    if ProgressForm <> nil then
      ProgressForm.Free;
  end;
end;
xueguangjin 2010/3/21 22:24:10
请求高人相助!
我问题太多!
songyang 2011/3/18 10:47:37
导出的字符型字段数据都变成64
我要发表评论 查看全部评论
 
  DELPHI盒子版权所有 1999-2023 V4.01 粤ICP备10103342号-1 更新RSS列表