无论如何,VB.NET Excel进程都不会关闭 [英] VB.NET Excel process won't close no matter what
问题描述
我在VB.NET环境中编程的应用程序遇到了问题,该应用程序必须打开Excel文件以获取一些数据,然后将其关闭.问题是,无论我做什么,在Windows的任务管理器中,应用程序始终都在其中.
I'm having problems with an application I programmed in VB.NET environment that has to open an Excel file to fetch some data and then close it. The problem is that no matter what I do, in Windows' task manager the application is always there.
应用程序的作用:
EA = New Excel.Application
OptionsSource = EA.Workbooks.Open(Filename:=myFileName, ReadOnly:=True)
wks = OptionsSource.Worksheets(1)
' reads data from cells and stores it in an array
wks = Nothing
OptionsSource.Close()
OptionsSource = Nothing
EA.Quit()
EA = Nothing
我尝试过的事情:
GC.Collect() ' after the .Close(), but really I tried it everywhere
Marshal.ReleaseComObject(Obj) ' after each Object
我所看到的是:
Application.Exit() ' seems like I cannot use it somehow
我能做些什么(但我还没有弄清楚怎么做):
What I could do (but I haven't yet figured out how):
Process.Kill ' I'd have to find exactly the process that is started by EA = New Excel.Application and then kill it
我绝对不能做的就是杀死所有被称为"EXCEL"的进程(尽管我知道该怎么做).
What I cannot definitely do is to kill all the processes that are called "EXCEL" (I know how to do this, though).
如何一劳永逸地攻击我的 Excel.Application
?
How do I nuke my Excel.Application
once and for all?
推荐答案
这是一种始终对我有效的谨慎(也许过于谨慎)的方法.我引用了每个COM对象,因此以后可以释放它们.在访问COM对象时,我避免使用多个.
.例如,而不是从 xlApp.Workbooks.Open(Filename:= FileName,ReadOnly:= true).xetsSheet(1)
,我每次只会上一层,所以我需要变量 xlApp
, xlBooks
, xlBook
和 xlSheet
.有人会说这是不必要的,但这不会打扰我,因为我已经成功地使用了这种方法.
This is a cautious (perhaps overly cautious) method which always works for me. I reference each COM object so I can release them later. I avoid having more than one .
when accessing a COM object i.e. instead of getting xlSheet from xlApp.Workbooks.Open(Filename:= FileName, ReadOnly:= true).Sheets(1)
, I only go one level each time, so I need the variables xlApp
, xlBooks
, xlBook
, and xlSheet
. Some people will say this is unnecessary, but it doesn't bother me because I have had success with this method.
这与您的操作方式相似.但是,如果没有其他要求,您可能至少要尝试 EA.Quit()
.
It's similar to how you do it anyway. But you may want to at least try EA.Quit()
if nothing else.
Dim xlApp As Microsoft.Office.Interop.Excel.Application = Nothing
Dim xlBooks As Microsoft.Office.Interop.Excel.Workbooks = Nothing
Dim xlBook As Microsoft.Office.Interop.Excel.Workbook = Nothing
Dim xlSheet As Microsoft.Office.Interop.Excel.Worksheet = Nothing
Dim xlRange As Microsoft.Office.Interop.Excel.Range = Nothing
Try
xlApp = New Microsoft.Office.Interop.Excel.Application()
xlBooks = xlApp.Workbooks
xlBook = xlBooks.Open(Filename:= FileName, ReadOnly:= true)
xlSheet = xlBook.Sheets(1)
' do other stuff
Finally
GC.Collect()
GC.WaitForPendingFinalizers()
If xlRange IsNot Nothing Then Marshal.ReleaseComObject(xlRange)
If xlSheet IsNot Nothing Then Marshal.ReleaseComObject(xlSheet)
If xlBook IsNot Nothing Then
xlBook.Close(SaveChanges:= false)
Marshal.ReleaseComObject(xlBook)
End If
If xlBooks IsNot Nothing Then
xlBooks.Close()
Marshal.ReleaseComObject(xlBooks)
End If
If xlApp IsNot Nothing
xlApp.Quit()
Marshal.ReleaseComObject(xlApp)
End If
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
这篇关于无论如何,VB.NET Excel进程都不会关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!