I have a C# program that opens an Excel file, reads a cell, closes the file, and exits Excel. Unfortunately, the Windows Task Manager still shows an Excel.exe process running. I've read just about every article concerning this issue and tried almost all of the solutions . . . and still have the same problem. I believe one of COM objects is not being released and thus hanging the process. However, I also believe that I've been very careful about instantiating the Excel objects (no double ".") and releasing them.
If I remove the "a = xlCells[1,1].Value" line, every thing releases and Excel dies cleanly after the FinalReleaseComObject of the Application instance. Why would this assignment create COM objects or interfere with them?
Excel.Application xlApp = null;
Excel.Workbooks xlWorkbooks = null;
Excel.Workbook xlWorkbook = null;
Excel.Sheets xlSheets = null;
Excel.Worksheet xlWorksheet = null;
Excel.Range xlCells = null;
string inputFile = @"C:\Temp\test.xlsx";
string a;
xlApp = new Excel.Application();
xlApp.Visible = false;
xlApp.DisplayAlerts = false;
xlWorkbooks = xlApp.Workbooks;
xlWorkbook = xlWorkbooks.Open(inputFile);
xlSheets = xlWorkbook.Sheets;
xlWorksheet = xlSheets[1];
xlCells = xlWorksheet.Cells;
a = xlCells[1,1].Value;
Marshal.FinalReleaseComObject(xlCells);
xlCells = null;
Marshal.FinalReleaseComObject(xlWorksheet);
xlWorksheet = null;
Marshal.FinalReleaseComObject(xlSheets);
xlSheets = null;
xlWorkbook.Close(false, Type.Missing, Type.Missing);
Marshal.FinalReleaseComObject(xlWorkbook);
xlWorkbook = null;
xlWorkbooks.Close();
Marshal.FinalReleaseComObject(xlWorkbooks);
xlWorkbooks = null;
xlApp.Quit();
Marshal.FinalReleaseComObject(xlApp);
xlApp = null;