有请高手帮忙—关于从DBGrid导入excel中出错。

yy99168 2006-06-06 04:07:47
导出函数:
procedure Tfrmuq.CopyDbDataToExcel(target: TDbGrid);
var
icount,jcount:integer;
xlApp: variant;
sheet:variant;
temp:string;
cnum:integer;
begin
savedialog1.Filter:='Excel文件(*.xls)|*.xls';
SaveDialog1.Options:=[ofShowHelp,ofEnableSizing];
if SaveDialog1.Execute then
begin
Screen.Cursor:=crHourGlass;
try
if not VarIsEmpty(xlApp) then
begin
xlApp.DisplayAlerts:=false;
xlApp.Quit;
VarClear(xlApp);
end;
///////////////////create excel object by ole
try
xlApp:=CreateOleObject('Excel.Application');
except
Screen.Cursor:=crDefault;
exit;
end;
xlApp.WorkBooks.Add[XLWBatWorkSheet];
xlApp.WorkBooks[1].WorkSheets[1].Name:='exlsheet';
sheet:=xlApp.WorkBooks[1].WorkSheets['exlsheet'];

if not target.DataSource.DataSet.Active then
begin
Screen.Cursor:=crDefault;
exit;
end;
target.DataSource.DataSet.First;
cnum:=1;
for icount:=0 to target.Columns.Count-1 do
if target.Columns[icount].Visible=true then
begin
sheet.cells[1,cnum]:=target.Columns.Items[cnum-1].Title.Caption;
inc(cnum);
end;
jcount:=2;
while not target.DataSource.DataSet.Eof do
begin
cnum:=1;
for icount:=0 to target.Columns.Count-1 do
begin
// if icount=0 then
// begin
if target.Columns[icount].Visible=true then
begin
temp:=target.Columns.Items[icount].Field.AsString;
insert('''',temp,1);
sheet.cells[jcount,cnum]:=temp;
inc(cnum);
end;
// end
// else //后面也可能有多位数字的字段
// sheet.cells[jcount,icount+1]:=target.Columns.Items[icount].Field.AsString;
end;
inc(jcount);
target.DataSource.dataset.Next;

end;
//begin
sheet.SaveAs(SaveDialog1.FileName);
application.MessageBox('数据导出成功!','提示信息',64+0);
//end;
xlApp.Quit;
SaveDialog1.Options:=[ofOverwritePrompt,ofShowHelp,ofEnableSizing];
finally
Screen.Cursor:=crDefault;
end;
end;
end;


出错提示:

project project1.exe raised exception class EAccessviolation with message 'Access violation address 00000000.Read of address 00000000'.Process stopped.use step or Run to continue.

请高手指点

...全文
194 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
yy99168 2006-06-07
  • 打赏
  • 举报
回复
为什莫运行不了呀???---------------------lzf1010(深宇)
深宇 2006-06-06
  • 打赏
  • 举报
回复
这个ProgressBarform函数放在ExportToExcel函数的前面就可以了

这两个函数可以单独编译的
yy99168 2006-06-06
  • 打赏
  • 举报
回复
function ProgressBarform(max:integer):tProgressBar;
这个函数是什么意思


请高手给我整个的这段程序unit 贴出来。
深宇 2006-06-06
  • 打赏
  • 举报
回复
我那个还有进度条,对用户友好很多
深宇 2006-06-06
  • 打赏
  • 举报
回复
用我这个吧
不过记得要uses comctrls,db,dbgrids,comobj
yy99168 2006-06-06
  • 打赏
  • 举报
回复
project project1.exe raised exception class EAccessviolation with message 'Access violation address 00000000.Read of address 00000000'.Process stopped.use step or Run to continue.

谁能给解释解释这个错误,最好对应我的源程序
深宇 2006-06-06
  • 打赏
  • 举报
回复
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.Smooth:=true;
ProgressBar1.Max:=max;
ProgressBar1.Parent:=form;
ProgressBar1.Height:=20;
ProgressBar1.Width:=250;
ProgressBar1.Left:=5;
ProgressBar1.Top:=5;
ProgressBar1.Step:=1;
form.Show;
result:=ProgressBar1;
end;
深宇 2006-06-06
  • 打赏
  • 举报
回复
function ExportToExcel(dbgrid:tdbgrid):boolean;
const
xlNormal=-4143;
var
i,j,k:integer;
str,filename:string;
excel:OleVariant;
SavePlace: TBookmark;
savedialog:tsavedialog;
ProgressBar1:TProgressBar;
begin
result:=false;
filename:='';
if not dbgrid.DataSource.DataSet.Active then
begin
application.MessageBox('数据集尚未打开!','错误',0);
exit;
end;
screen.Cursor:=crHourGlass;
try
excel:=CreateOleObject('Excel.Application');
excel.workbooks.add;
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;
if filename='' then
begin
result:=true;
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
k:=0;
for i:=0 to dbgrid.Columns.count-1 do
begin
if dbgrid.Columns.Items[i].Visible then
begin
//Excel.Columns[k+1].ColumnWidth:=dbgrid.Columns.Items[i].Title.Column.Width;
excel.cells[1,k+1]:=dbgrid.Columns.Items[i].Title.Caption;
inc(k);
end;
end;

dbgrid.DataSource.DataSet.DisableControls;
saveplace:=dbgrid.DataSource.DataSet.GetBookmark;
dbgrid.DataSource.dataset.First;
i:=2;

ProgressBar1:=ProgressBarform(dbgrid.DataSource.DataSet.RecordCount);
while not dbgrid.DataSource.dataset.Eof do
begin
k:=0;
for j:=0 to dbgrid.Columns.count-1 do
begin
if dbgrid.Columns.Items[j].Visible then
begin
//excel.cells[i,k+1].NumberFormat:='@';
if not dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).isnull then
begin
str:=dbgrid.DataSource.dataset.fieldbyname(dbgrid.Columns.Items[j].FieldName).value;
Excel.Cells[i, k + 1] := Str;
end;
inc(k);
end
else
continue;
end;
inc(i);
ProgressBar1.StepBy(1);
dbgrid.DataSource.dataset.next;
end;
progressbar1.Parent.Free;

dbgrid.DataSource.dataset.GotoBookmark(SavePlace);
dbgrid.DataSource.dataset.EnableControls;

try
if copy(FileName,length(FileName)-3,4)<>'.xls' then
FileName:=FileName+'.xls';
Excel.ActiveWorkbook.SaveAs(FileName, xlNormal, '', '', False, False);
except
Excel.Quit;
screen.cursor:=crDefault;
exit;
end;
Excel.Visible := true;
screen.cursor:=crDefault;
Result := true;
end;
深宇 2006-06-06
  • 打赏
  • 举报
回复
坐在沙发上看问题

2,496

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 数据库相关
社区管理员
  • 数据库相关社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧