【发布时间】:2012-03-27 02:24:29
【问题描述】:
我在 .net 中使用与 office excel 互操作时遇到问题。我尝试了很多方法来关闭我创建的要在我的程序中使用的 excel 应用程序、工作簿和工作表,但我总是注意到 excel.exe 仍在内存中。我什至尝试过强制垃圾收集器,请帮忙。
这是我用来实例化所有内容的代码:
Private mExcelApp As Microsoft.Office.Interop.Excel.ApplicationClass
Private mWorkBook As Microsoft.Office.Interop.Excel.Workbook
Private mWorkSheet As Microsoft.Office.Interop.Excel.Worksheet
Public Sub Execute()
mExcelApp = New Microsoft.Office.Interop.Excel.ApplicationClass
If mFirstPass Then
mWorkBook = mExcelApp.Workbooks.Add()
mWorkSheet = CType(mWorkBook.ActiveSheet(), Microsoft.Office.Interop.Excel.Worksheet)
Else
mWorkBook = mExcelApp.Workbooks.Open(System.IO.Path.Combine(mFileLocation, mMTDefinition.Description & "_" & mMTDefinition.Version & ".xls"))
mWorkSheet = CType(mWorkBook.Sheets(1), Microsoft.Office.Interop.Excel.Worksheet)
Dim excelRange As Microsoft.Office.Interop.Excel.Range = mWorkSheet.UsedRange
excelRange.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell).Activate()
mCurrentRow = mExcelApp.ActiveCell.Row + 1
End If
Here is how i try to close everything
If mFirstPass Then
mWorkBook.SaveAs(System.IO.Path.Combine(mFileLocation, mMTDefinition.Description & "_" & mMTDefinition.Version & ".xls"))
mFirstPass = False
Else
mWorkBook.Save()
End If
a
mWorkBook.Close()
mExcelApp.Quit()
mWorkSheet = Nothing
mWorkBook = Nothing
mExcelApp = Nothing
System.GC.Collect()
【问题讨论】:
-
你试过
for each book in mExcelApp.Workbooks: book.saved = true: next吗?
标签: .net vb.net office-interop