And*_* A. 2 c# asp.net excel interop
可能重复:
如何在C#中正确清理Excel互操作对象
我用这个函数计算一些数据的线性趋势:
private string Trend(object conocido_y, object conocido_x, object nueva_matriz_x)
{
string result = String.Empty;
try {
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
result = ((Array)xlApp.WorksheetFunction.Trend(conocido_y, conocido_x, nueva_matriz_x, true)).GetValue(1).ToString();
xlApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp);
xlApp = null;
}
catch (System.Runtime.InteropServices.COMException ex) {
DError.ReportarError(ex, false);
}
catch (Exception ex) {
DError.ReportarError(ex);
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
结果很好,但excel应用程序没有关闭,如果我打开任务管理器进程仍在运行,为什么?
我记得已经看到,在ReleaseComObject()之后,强制GC传递是由于对象被释放而excel最终死亡.
另外,我没有在那个片段中看到它,但你必须在任何工作表或其他Excel对象中使用ReleaseComObject()你可能已经得到了句柄(结果是这样的事情?).
ReleaseComObject(result);
app.Aplication.Quit();
ReleaseComObject(app);
GC.Collect();
Run Code Online (Sandbox Code Playgroud)