我在这里查看了一些关于在我的程序运行后摆脱 excel 实例的提示,但这些建议似乎都不起作用。当我最初运行它时,它将创建一个 excel 实例,但是当程序仍在运行时,我通过单击按钮重新运行此代码;它将创建另一个 excel 实例,但这一次它删除了它创建的实例,只留下了最初运行程序时创建的实例。
到目前为止,我所拥有的代码是这样的:(截至 2012 年 9 月 14 日的更新代码)
Private Sub GetBatchFileContents()
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlWS As Excel.Worksheet
Dim xlRan As Excel.Range
Dim xlVal(,) As Object
Dim lastRow As Int32
xlApp = New Excel.Application()
xlWB = xlApp.Workbooks.Open(TextBox1.Text.ToString(), _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing, _
Type.Missing)
xlWS = xlWB.Worksheets.Item(1)
lastRow = xlWS.Cells(xlWS.Rows.Count, 1).End(Excel.XlDirection.xlUp).Row
xlRan = xlWS.Range(xlWS.Cells(1, 1), xlWS.Cells(lastRow, 130))
xlVal = xlRan.Value2()
ReleaseObj(xlRan)
ReleaseObj(xlWS)
xlWB.Close(False, Type.Missing, Type.Missing)
ReleaseObj(xlWB)
xlApp.Quit()
ReleaseObj(xlApp)
End Sub
Private Sub ReleaseObj(ByRef obj As Object)
Try
Marshal.FinalReleaseComObject(obj)
Catch ex As Exception
Stop
Finally
obj = Nothing
End Try
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
End Sub
提前感谢您的反馈!