如何将delphi的dbgrid导出为excel表

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

interface

uses

forms,SysUtils,ComCtrls,DBGrids,DB,Dialogs,Messages,Windows,ComObj,Cont rols,ADODB,StdCtrls,Graphics;

function ProgressBarform(max:integer):tProgressBar;

function ExportToExcel(dbgrid:tdbgrid):boolean;

implementation

uses unit1;

//生成一个显示进度条的窗体

function ProgressBarform(max:integer):tProgressBar;

var

ProgressBar1:TProgressBar;

form:tform;

begin

application.CreateForm(tform,form);

form.Position:=poScreenCenter;

form.BorderStyle:=bsnone;

form.Height:=30;

form.Width:=260;

ProgressBar1:=TProgressBar.Create(form);

ProgressBar1.Visible:=true;

ProgressBar1.Smooth:=true;

ProgressBar1.Max:=max;

ProgressBar1.ParentWindow:=form.Handle;

ProgressBar1.Height:=20;

ProgressBar1.Width:=250;

ProgressBar1.Left:=form.Left+5;

ProgressBar1.Top:=form.Top+5;

ProgressBar1.Step:=1;

form.show;

result:=ProgressBar1;

end;

//将DBGRID中的内容导入到EXCEL中

function ExportToExcel(dbgrid:tdbgrid):boolean;

const

xlNormal=-4143;

var

i,j,k:integer;

str,filename:string;

excel:OleVariant;

SavePlace: TBookmark;

savedialog:tsavedialog;

ProgressBar1:TProgressBar;

result:=false;

filename:='';

if then

begin

if application.messagebox('需要导出的数据过大,Excel最

大只能容纳65536行,是否还要继续?','询问',mb_yesno+mb_iconquestion)=idno then

exit;

end;

screen.Cursor:=crHourGlass;

try

excel:=CreateOleObject('Excel.Application');

;

except

screen.cursor:=crDefault;

showmessage('无法调用Excel!');

exit;

end;

savedialog:=tsavedialog.Create(nil);

savedialog.Filter:='Excel文件(*.xls)|*.xls';

if savedialog.Execute then

begin

if FileExists(savedialog.FileName) then

try

if application.messagebox('该文件已经存在,要覆盖吗?','询问',mb_yesno+mb_iconquestion)=idyes then

DeleteFile(PChar(savedialog.FileName))

else

begin

Excel.Quit;

savedialog.free;

screen.cursor:=crDefault;

Exit;

end;

except

Excel.Quit;

savedialog.free;

screen.cursor:=crDefault;

Exit;

end;

filename:=savedialog.FileName;

end;

savedialog.free;

application.ProcessMessages;

if filename='' then

begin

result:=false;

Excel.Quit;

screen.cursor:=crDefault;

exit;

end;

k:=0;

for i:=0 to do

begin

if then

begin

//Excel.Columns

[k+1].ColumnWidth:=;

excel.cells[1,k+1]:=

[i].Title.Caption;

inc(k);

end;

end;

;

saveplace:=;

;

i:=2;

if then

ProgressBar1:=ProgressBarform(65536)

else

ProgressBar1:=ProgressBarform

(;

while not do

begin

k:=0;

for j:=0 to do

begin

if then

begin

excel.cells[i,k+1].NumberFormat:='@';

if not

[j].FieldName).isnull then

begin

str :=

[j].FieldName).value;

Excel.Cells[i, k + 1] := Str;

相关文档
最新文档