我应该如何申报Microsoft.Office.Interop.Excel.Worksheet这样我就可以在C#中关闭它? [英] How should I declare a Microsoft.Office.Interop.Excel.Worksheet so I can close it in C#?

查看:499
本文介绍了我应该如何申报Microsoft.Office.Interop.Excel.Worksheet这样我就可以在C#中关闭它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很难确保我的COM对象被关闭,所以我不会离开在后台运行的进程EXCEL.EXE。我了解到,申报的双点链接是坏的,因为这可能使游逛COM对象。

I am having a hard time making sure my COM objects are closing so I am not leaving an EXCEL.EXE process running in the background. I have learned that the double dot and chaining of declarations is bad as this could leave COM objects hanging around.

有谁知道如何解决这行代码这样我就可以正确地关闭和释放工作表COM对象?

Does anyone know how to fix this line of code so I can properly close and release the worksheet COM object?

worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[strName];



编辑:我一直在使用app.Quit()尝试当我执行完这条线关闭打开Excel对象,但是这是行不通的。 app.Quit()的作品,如果我然而这行之前调用它。

I have tried using app.Quit() when I am done executing this line to close the open Excel object, however this does not work. app.Quit() works if I call it before this line however.

推荐答案

首先,为了让EXCEL .EXE程序退出你没有明确地释放你使用的所有的COM对象。这是由当您获得另一个引用COM对象的.NET运行时为您隐式创建运行时可调用包装(RCW)是由GC收集和发布此基础COM对象。

First of all, in order to let the EXCEL.EXE process quit you do not have to explicitly release all COM objects you used. The Runtime Callable Wrappers (RCW) which are implicitly created for you by the .NET runtime when you obtain another reference to a COM object are collected by GC and this releases underlying COM objects.

所有你所要做的就是调用Quit方法,释放的RCW的引用,让GC收集它们。

All you have to do is to call the Quit method, release RCWs references and let GC collect them.

//This runs the EXCEL.EXE process.
Microsoft.Office.Interop.Excel.Application app = 
    new Microsoft.Office.Interop.Excel.Application();
//This locks the excel file either for reading or writing 
//depending on parameters.
Microsoft.Office.Interop.Excel.Workbook book = app.Workbooks.Open(...);

...

//Explicitly closing the book is a good thing. EXCEL.EXE is alive 
//but the excel file gets released.
book.Close();

//This lets the EXCEL.EXE quit after you release all your references to RCWs
//and let GC collect them and thus release the underlying COM objects. 
//EXCEL.EXE does not close immediately after this though.
app.Quit();
app = null;



二,如果你离开所谓的双点行代码是,你没有创建任何内存泄漏。垃圾收集器将收集的RCW,当它发现它的权利将某个时候释放COM对象即

Second, if you leave so-called double-dot code lines be, you do not create any memory leakage. Garbage collector will collect the RCWs and will release the COM objects sometime i.e. when it finds it right.

最后,如果你想明确地释放的RCW和相应的COM对象最大限度地降低不惜一切代价的内存压力,你可以显式调用GC收集的RCW你释放你的引用后,他们或之前GC收集的RCW甚至明确地释放潜在的COM对象。不过要小心。这最后一种方法让你在剩余的RCW从不之后使用的,或者你将有一个例外的事实负责。

And finally if you wish to release the RCWs and corresponding COM objects explicitly to minimize the memory pressure at all costs, you could explicitly call the GC to collect the RCWs after you release your references to them or even explicitly release the underlying COM objects BEFORE the GC collects the RCWs. Be careful though. This last way leaves you in responsibility for the fact that the remaining RCWs are never used after that or you are going to have an exception.

using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

Application app = new Application();
Workbooks books = clsExcelApplication.Workbooks;
Workbook book = books.Open("...");
Sheets sheets = book.Sheets;
Worksheet sheet = sheets["..."];

...

//Trying to be as explicit as we can.
book.Сlose();

//Revese order. Using FinalReleaseComObject is even more dangerous. 
//You might release an object used inside excel
//code which might lead to some dreadful internal exceptions.
int remainingRefCount;
remainingRefCount = Marshal.ReleaseComObject(sheet);
remainingRefCount = Marshal.ReleaseComObject(sheets);
remainingRefCount = Marshal.ReleaseComObject(book);
remainingRefCount = Marshal.ReleaseComObject(books);
app.Quit();
remainingRefCount = Marshal.ReleaseComObject(app);



记住。如果你手动释放COM引用超过EXCEL.EXE一辈子不依赖于的RCW,当你喜欢,你可以抵消他们...

Keep in mind. If you manually release COM references than EXCEL.EXE lifetime does not depend on the RCWs and you can nullify them when you like...

sheet = null;
sheets = null;
book = null;
books = null;
app = null;

在最明确的情况下,不要忘记检查从Marshal.ReleaseComObject的回报。如果不是零,别人拿着一个COM引用您的基础COM对象除了你刚刚发布的RCW。

In the most explicit case, do not forget to check the return from the Marshal.ReleaseComObject. If it is not zero, somebody else is holding a COM reference to your underlying COM object besides the RCW you just released.

这篇关于我应该如何申报Microsoft.Office.Interop.Excel.Worksheet这样我就可以在C#中关闭它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆