Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem closing excel interop app properly in .net

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()
like image 326
Thulani Chivandikwa Avatar asked Feb 25 '11 09:02

Thulani Chivandikwa


People also ask

How do I stop excel in C#?

workbook. Close(0); excelApp. Quit(); Worked for me.

How do I use Microsoft Office Interop Excel in .NET core?

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.

Do I need to have Office installed to use Microsoft Office Interop Excel DLL?

Yes, you are right. You need Excel to be installed to use the Excel Manipulation feature with Microsoft. Office.

How do I use Microsoft Office Interop repair tool?

Using the Repair Tool for Microsoft Office InteropSelect Home (Studio Backstage View) > Tools > Repair Tool for Microsoft Office Interop. The repair tool opens.


2 Answers

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
like image 163
Justin Largey Avatar answered Sep 28 '22 23:09

Justin Largey


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.

like image 44
SWeko Avatar answered Sep 29 '22 01:09

SWeko