Excel.Range对象未处置,因此无法关闭Excel进程 [英] Excel.Range object not disposing so not Closing the Excel process
问题描述
几个小时以来,我一直在努力工作,以处置由下面的语句创建的Excel.Range对象.我已经检查了关于堆栈溢出的几乎所有讨论.
I am banging my head since hours to dispose the Excel.Range Object creating by the statement below. I Have checked almost all the Discussions regarding it on Stack Overflow.
oSheet.Range("A1", "H1").Merge()
当我注释掉此行时,Excel应用程序成功关闭.但是,当我使用此行时,它将创建Excel.Range对象,该对象不会由GC处理.谁能帮帮我.
When i comment out this line Excel Application closes successfully. But when i use this line it creates the Excel.Range object which is not disposing of by GC. Can anyone please help me out.
谢谢
推荐答案
您不应执行此类嵌套调用,而应将其更改为:
You shouldn't do nested calls like that, instead change it to be something like:
Dim r As Object = oSheet.Range("A1", "H1")
r.Merge()
然后可以使用以下方法正确处理r:
Then r can be disposed properly by a:
Marshal.ReleaseComObject(r)
然后,您可以根据需要重新使用变量:
And then you can reuse the variable if you want:
r = oSheet.Cells(TitleRow3, 1)
' do something with r
Marshal.ReleaseComObject(r)
r = 'get a new range from somewhere
' do something with r
Marshal.ReleaseComObject(r)
...
这篇关于Excel.Range对象未处置,因此无法关闭Excel进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!