有请高手帮忙—关于从DBGrid导入excel中出错。
导出函数:
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.
请高手指点