再以下这段代码最后如何释放Excel? procedure Tmainform.CopyDbDataToExcel(Target: Tiwdbgrid);
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;begin
//Screen.Cursor := crHourGlass;
//if not VarIsEmpty(XLApp) then
//begin
//XLApp.DisplayAlerts := False;
//XLApp.Quit;
//VarClear(XLApp);
//end;
//通过ole创建Excel对象
try
XLApp := CreateOleObject('Excel.Application');
except
Exit;
end;
XLApp.WorkBooks.Add[XLWBatWorksheet];
XLApp.WorkBooks[1].WorkSheets[1].Name := '帐务中心提供 web';
Sheet := XLApp.Workbooks[1].WorkSheets['帐务中心提供 web'];if not Target.DataSource.DataSet.Active then
beginExit;
end;Target.datasource.DataSet.first;for iCount := 0 to Target.Columns.Count - 1 do
begin
Sheet.cells[1, iCount + 1] := Target.Columns.Items[icount].DisplayName;
end; //Target.Columns.Items[iCount].Title.Caption;
jCount := 1;
while not Target.DataSource.DataSet.Eof do
begin
for iCount := 0 to Target.Columns.Count - 1 do
begin
Sheet.cells[jCount + 1, iCount + 1] :=target.DataSource.DataSet.FieldByName(Target.Columns.Items[icount].DisplayName).AsString;
end; //Target.Columns.Items[iCount].Field.AsString
Inc(jCount);
Target.DataSource.DataSet.Next;
end;
//XlApp.Visible := True;
//webapplication.showmessage('c:\downfiles\'+filestr+'.xls');
getfilestr;
XLApp.activeworkbook.saveas('c:\downfiles\'+filestr+'.xls');
//sheet.close;
XLApp.application.Quit;//好像不起作用
xlapp:=Unassigned; //提示Unassigned是不认识的标识符WebApplication.SendFile('c:\downfiles\'+filestr+'.xls',True , '', '');//如果不释放excel这里就要提示文件占用错误~ 请教如何办?
end;
var
iCount, jCount: Integer;
XLApp: Variant;
Sheet: Variant;begin
//Screen.Cursor := crHourGlass;
//if not VarIsEmpty(XLApp) then
//begin
//XLApp.DisplayAlerts := False;
//XLApp.Quit;
//VarClear(XLApp);
//end;
//通过ole创建Excel对象
try
XLApp := CreateOleObject('Excel.Application');
except
Exit;
end;
XLApp.WorkBooks.Add[XLWBatWorksheet];
XLApp.WorkBooks[1].WorkSheets[1].Name := '帐务中心提供 web';
Sheet := XLApp.Workbooks[1].WorkSheets['帐务中心提供 web'];if not Target.DataSource.DataSet.Active then
beginExit;
end;Target.datasource.DataSet.first;for iCount := 0 to Target.Columns.Count - 1 do
begin
Sheet.cells[1, iCount + 1] := Target.Columns.Items[icount].DisplayName;
end; //Target.Columns.Items[iCount].Title.Caption;
jCount := 1;
while not Target.DataSource.DataSet.Eof do
begin
for iCount := 0 to Target.Columns.Count - 1 do
begin
Sheet.cells[jCount + 1, iCount + 1] :=target.DataSource.DataSet.FieldByName(Target.Columns.Items[icount].DisplayName).AsString;
end; //Target.Columns.Items[iCount].Field.AsString
Inc(jCount);
Target.DataSource.DataSet.Next;
end;
//XlApp.Visible := True;
//webapplication.showmessage('c:\downfiles\'+filestr+'.xls');
getfilestr;
XLApp.activeworkbook.saveas('c:\downfiles\'+filestr+'.xls');
//sheet.close;
XLApp.application.Quit;//好像不起作用
xlapp:=Unassigned; //提示Unassigned是不认识的标识符WebApplication.SendFile('c:\downfiles\'+filestr+'.xls',True , '', '');//如果不释放excel这里就要提示文件占用错误~ 请教如何办?
end;
xlapp:=Unassigned;
在
WebApplication.SendFile('c:\downfiles\'+filestr+'.xls',True , '', '');
前加一句
showmessge('ok');
这样应该就可以了
改成:
XLApp.Quit;//好像就起作用了
XLApp.Quit;
xlapp:=Unassigned; 之后同样提示文件还在被使用,进程里面的Execel.exe还在,晕
XLApp.application.Quit;
XLApp.Quit;
xlapp:=Unassigned;