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.
我在。net中使用office excel的interop有问题。我尝试了很多东西来关闭excel应用程序、工作簿和工作表,但我总是注意到excel。exe仍然在内存中。我甚至尝试过强制垃圾回收,请帮忙。
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()
2 个解决方案
#1
5
The basic idea is to call Marshal.ReleaseComObject for every COM object created.
基本的想法是调用Marshal。为创建的每个COM对象释放一个ecomobject。
Dim app As New Excel.Application()
Dim workBook As Excel.Workbook = app.Workbooks.Add()
Try
Dim t As Int32 = 0
' This example is filling an excel spreadsheet using data stored in a Dictionary
For Each key In sampleData.Keys
t += 1
' Add a worksheet and dump data.
Dim sheet As Excel.Worksheet = workBook.Worksheets.Add()
sheet.Name = key
' Set columns
For i = 1 To sampleData(key).Columns.Count
sheet.Cells(1, i) = sampleData(key).Columns(i - 1).ColumnName
Next
' Set data.
For r = 1 To sampleData(key).Rows.Count
For c = 1 To sampleData(key).Columns.Count
sheet.Cells(r + 1, c) = sampleData(key).Rows(r - 1).Item(c - 1).ToString()
Next c
Next r
Marshal.ReleaseComObject(sheet)
Next
workBook.SaveAs("fileName.xls", Excel.XlFileFormat.xlExcel8)
workBook.Close(SaveChanges:=False)
Finally
app.Quit()
Marshal.ReleaseComObject(workbook)
Marshal.ReleaseComObject(app)
app = Nothing
End Try
#2
2
As the excel application is a unmanaged COM object, reclaiming the managed memory might not work. I've used
由于excel应用程序是一个非托管COM对象,回收托管内存可能无法工作。我使用
Marshal.ReleaseComObject(mExcelApp);
with some success.
与一些成功。
#1
5
The basic idea is to call Marshal.ReleaseComObject for every COM object created.
基本的想法是调用Marshal。为创建的每个COM对象释放一个ecomobject。
Dim app As New Excel.Application()
Dim workBook As Excel.Workbook = app.Workbooks.Add()
Try
Dim t As Int32 = 0
' This example is filling an excel spreadsheet using data stored in a Dictionary
For Each key In sampleData.Keys
t += 1
' Add a worksheet and dump data.
Dim sheet As Excel.Worksheet = workBook.Worksheets.Add()
sheet.Name = key
' Set columns
For i = 1 To sampleData(key).Columns.Count
sheet.Cells(1, i) = sampleData(key).Columns(i - 1).ColumnName
Next
' Set data.
For r = 1 To sampleData(key).Rows.Count
For c = 1 To sampleData(key).Columns.Count
sheet.Cells(r + 1, c) = sampleData(key).Rows(r - 1).Item(c - 1).ToString()
Next c
Next r
Marshal.ReleaseComObject(sheet)
Next
workBook.SaveAs("fileName.xls", Excel.XlFileFormat.xlExcel8)
workBook.Close(SaveChanges:=False)
Finally
app.Quit()
Marshal.ReleaseComObject(workbook)
Marshal.ReleaseComObject(app)
app = Nothing
End Try
#2
2
As the excel application is a unmanaged COM object, reclaiming the managed memory might not work. I've used
由于excel应用程序是一个非托管COM对象,回收托管内存可能无法工作。我使用
Marshal.ReleaseComObject(mExcelApp);
with some success.
与一些成功。