再以下这段代码最后如何释放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;

解决方案 »

  1.   

    删掉
    xlapp:=Unassigned; 

    WebApplication.SendFile('c:\downfiles\'+filestr+'.xls',True , '', '');
    前加一句
    showmessge('ok');
    这样应该就可以了
      

  2.   

    XLApp.application.Quit;//好像不起作用
    改成:
    XLApp.Quit;//好像就起作用了
      

  3.   

    XLApp.application.Quit;//好像不起作用->>>>XLApp.Quit;xlapp:=Unassigned;  //提示Unassigned是不认识的标识符->> 有没有包含System单元?
      

  4.   

    感谢大家帮忙~~!!! xlapp:=unassigned 必须要uses Variants单元,不是system单元但是就算加入了
    XLApp.Quit;
    xlapp:=Unassigned; 之后同样提示文件还在被使用,进程里面的Execel.exe还在,晕 
      

  5.   

    感谢大家~搞定了,原来必须要这三语句一起上阵才行,缺一都不能释放
    XLApp.application.Quit;
    XLApp.Quit;
    xlapp:=Unassigned;