Excel Interop意外地打开了第二个Excel实例

我正在尝试在一个IDisposable接口中包装一个Excel.ApplicationClass ,使其在使用后自动closures。 我所拥有的东西如下所示:

 module Excel = type Application() = member private this.excel = new Excel.ApplicationClass() interface IDisposable with member this.Dispose() = this.excel.Quit() Marshal.ReleaseComObject(this.excel) |> ignore 

当我在像一个函数中调用它

 let func = use ex = new Excel.Application() () 

两个Excel实例被启动(我可以在我的任务pipe理器中看到),但是只有其中一个被再次closures。 有谁能告诉我我在做什么错在这里?

您的this.excel属性会在每次评估时创build一个新的Excel进程,因此调用Dispose会创build一个进程,然后立即退出,另一个进程仅用于调用Marshal.ReleaseComObject 。 第二个可能是一个活着的人。

改变你的代码是这样的:

 module Excel = type Application() = let m_excel = new Excel.ApplicationClass() member private this.excel = m_excel interface IDisposable with member this.Dispose() = this.excel.Quit() Marshal.ReleaseComObject(this.excel) |> ignore