C#如何阻止用户使用程序创build的Excel应用程序

基本上我有一个使用Excel应用程序的C#编写的程序。

当运行我的程序时,如果用户试图打开任何Excel文件,该文件在我的程序创build的Excel实例上打开。

我想这与excel打开到“Excel.exe”的现有实例有关。

当我的程序退出时,它导致僵尸excel过程。

我想要做的是隐藏我的Excel实例被用户使用。 (或针对这种情况的任何其他解决scheme)

我试图谷歌的东西,但没有到目前为止。

我的代码看起来像这样。

foreach (string strFileName in this.m_ListRequestFileNames) { object misVal = System.Reflection.Missing.Value; try { if (Common.IsOpened(strFileName) == true) { this.m_ParentWnd.Log(strFileName + " is in use by another program." + Environment.NewLine); continue; } this.m_xlWBook = this.m_xlApp.Workbooks.Open(strFileName, 3, true, misVal, misVal, misVal, misVal, misVal, misVal, false, misVal, misVal, misVal, misVal, misVal); if (Make(ref m_xlWBook, m_strDstPath, ref m_ListIgnoreSheetNames) != true) { this.m_ParentWnd.Log("-!- Error while making " + m_xlWBook.Name + " into a csv file" + Environment.NewLine); continue; } this.m_ParentWnd.Log("Completed " + m_xlWBook.Name + Environment.NewLine); } catch (System.Exception e) { m_ParentWnd.Log(e.ToString()); } this.m_xlWBook.Close(false, null, null); } this.m_xlApp.Quit(); Common.releaseObject(this.m_xlApp); 

至less从我的代码中可以看出,问题在于,一旦完成(app.Quit),就不会closuresExcel了 – 如果是的话,那么您可能不会处理创build的对象。 使用Excel互操作,你不在快乐的垃圾收集,你必须小心清理自己。 一个被遗忘的小例子将会让Excel在后台处理你所描述的恼人的副作用。 看看这个StackOverflow线程进行了广泛的讨论: 如何正确地清理Excel互操作对象?