c# - 为什么 excel 保持打开状态?

标签 c# asp.net excel interop

<分区>

Possible Duplicate:
How to properly clean up Excel interop objects in C#

我有这个函数用来计算一些数据的线性趋势:

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;
}

结果很好,但 excel 应用程序没有关闭,如果我打开任务管理器,进程仍在运行,为什么?

最佳答案

我记得曾经看到过,在 ReleaseComObject() 之后,强制 GC 通过是由于对象被释放并且 excel 最终死亡。

此外,我在那个片段中没有看到它,但是您必须在任何工作表或您可能已经处理过的其他 Excel 对象中使用 ReleaseComObject()(结果是这样的吗?)。

ReleaseComObject(result);
app.Aplication.Quit();
ReleaseComObject(app);
GC.Collect();

关于c# - 为什么 excel 保持打开状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1884217/

相关文章:

excel - 来自 excel 的 CSV 文件,其中双引号内的字段

c# - 在 .Net c# 中使用 OpenXML 在页脚中插入图像

c# - ASP.Net LinkBut​​ton 在本地工作但不能在服务器上工作

vba - 使用 VBA 从 Excel 激活 Word 窗口

c# - 如何在新数据列中设置格式日期时间

c# - 简单的评估表不显示在表中

arrays - 你可以直接将excel VBA中的数组内容写入CSV文件吗?

c# - 如何使用子绑定(bind)中的父内容控件?

c# - 如何在使用 FileSystemWatcher 监视多个文件夹的应用程序中监视多个共享文件

c# - ServiceHost 只支持类服务类型