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()
workbook. Close(0); excelApp. Quit(); Worked for me.
Office. Interop. Excel' nuget package, and then add reference to 'Microsoft Excel 16.0 Object Library' (right click the project -> 'Add' -> 'Reference' -> search and add 'Microsoft Excel 16.0 Object Library' ). Click ''Interop.
Yes, you are right. You need Excel to be installed to use the Excel Manipulation feature with Microsoft. Office.
Using the Repair Tool for Microsoft Office InteropSelect Home (Studio Backstage View) > Tools > Repair Tool for Microsoft Office Interop. The repair tool opens.
The basic idea is to call Marshal.ReleaseComObject for every COM object created.
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
As the excel application is a unmanaged COM object, reclaiming the managed memory might not work. I've used
Marshal.ReleaseComObject(mExcelApp);
with some success.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With