【问题标题】:I am facing with the following error in Windows Form Application ? Can anyone help to resolve it我在 Windows 窗体应用程序中遇到以下错误?任何人都可以帮助解决它
【发布时间】:2019-03-07 10:16:13
【问题描述】:

从excel上传数据并存入gridview后,关闭应用程序,完成所有任务后出现如下弹窗。enter image description here

已经编写了 excel wokbook 和 excel sheet 的代码来关闭 即:sWorkbook.Close(); sExcelApp.Quit();

【问题讨论】:

  • 请向我们展示与使用 Excel 对象模型中的任何内容相关的所有代码。

标签: c# .net excel windows forms


【解决方案1】:

以上消息是由于您的 excel 对象没有关闭它仍然保留在系统进程中。

您实际上可以干净地释放您的 Excel 应用程序对象,但您必须小心。 您访问的 COM 对象然后通过Marshal.FinalReleaseComObject() 显式释放它在理论上是正确的,但不幸的是,在实践中很难管理。

// Cleanup
GC.Collect();
GC.WaitForPendingFinalizers();

Marshal.FinalReleaseComObject(xlRng);
Marshal.FinalReleaseComObject(xlSheet);

xlBook.Close(Type.Missing, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(xlBook);

xlApp.Quit();

Marshal.FinalReleaseComObject(xlApp); 在大多数代码示例中,您将看到从 .NET 中清除 COM 对象,GC.Collect()GC.WaitForPendingFinalizers() 调用两次,如下所示:

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();

【讨论】:

    猜你喜欢
    • 2012-11-30
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 2015-03-15
    • 2013-12-16
    • 2014-04-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多