1

I have the following blocks of code:

    Excel.Application xlApp = new Excel.Application();
    Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(this.pathToFile);
    Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
    Excel.Range xlRange = xlWorksheet.UsedRange;

I push the xlRange into an array using:

someArray = (System.Object[,])xlRange.Value2;

Afterwards I try to cleanup with:

    Marshal.ReleaseComObject(xlRange);
    Marshal.ReleaseComObject(xlWorksheet);

    xlWorkbook.Close();
    Marshal.ReleaseComObject(xlWorkbook);
    xlApp.Quit();
    Marshal.ReleaseComObject(xlApp);

However, even with all four of the excel objects being released with Marshal, the process stays open and prevents the file from being opened again.

Any ideas as to how to properly clean up Interop excel objects?

Giardino
  • 1,367
  • 3
  • 10
  • 30
  • 3
    Hae a look [at the answer to this question](http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects?rq=1) Your problem is that you are using Workbook.Open, Try Excel.Workbooks xlWorkbooks = xlApp.Workbooks, Excel.Worbook = xlWorkbooks.Open(this,pathtofile); then make sure to add release workbooks, and set null everything and run garbage collector. Same with your Sheets and worksheet. With com object NEVER have 2 dots on the right side. – user2140261 Feb 27 '14 at 17:29
  • 2
    I guess this is a similar question http://stackoverflow.com/questions/6960693/excel-process-doesnt-get-closed. Did you try the solution suggested there ? – Uroboros Feb 27 '14 at 16:39

1 Answers1

0

I had a similar issue and managed to close the application following this processs;

GC.Collect();
GC.WaitForPendingFinalizers();

Marshal.FinalReleaseComObject(xlRange);
Marshal.FinalReleaseComObject(xlWorksheet);

xlWorkbook.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(xlWorkbook);

xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
mark_h
  • 5,233
  • 4
  • 36
  • 52