5

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()
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
Thulani Chivandikwa
  • 3,402
  • 30
  • 33
  • have you tried `for each book in mExcelApp.Workbooks: book.saved = true: next`? –  Feb 25 '11 at 09:43

2 Answers2

5

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
Justin Largey
  • 1,934
  • 1
  • 16
  • 21
2

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.

SWeko
  • 30,434
  • 10
  • 71
  • 106