在 .net 中正确关闭 Excel 互操作应用程序时出现问题
我在 .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()
I am having issues using interop with office excel in .net. I have tried a lot of things to close the excel application, workbook and worksheets i create to use in my program but i always notice the excel.exe still in memory. I have even tried forcing the garbage collector, please help.
Here is the code i use to instantiate everything:
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
基本思想是为创建的每个 COM 对象调用 Marshal.ReleaseComObject。
The basic idea is to call Marshal.ReleaseComObject for every COM object created.
由于 Excel 应用程序是非托管 COM 对象,因此回收托管内存可能不起作用。我已经使用
并取得了一些成功。
As the excel application is a unmanaged COM object, reclaiming the managed memory might not work. I've used
with some success.