Excel process still runs in background
.net, c#, excel
Solution
There was another similar question - and answer (https://stackoverflow.com/a/17367570/3063884), in which the solution was to avoid using double-dot notation. Instead, define variables for each object used along the way, and individually use `Marshal.ReleaseComObject` on each one.
Copying straight from the linked solution:
var workbook = excel.Workbooks.Open(/*params*/)
---> instead use -->
var workbooks = excel.Workbooks;
var workbook = workbooks.Open(/*params*/)
Then, when done, release each COM object:
Marshal.ReleaseComObject(workbook);
Marshal.ReleaseComObject(workbooks);
Marshal.ReleaseComObject(excel);
Problem
I'm reading some data from a sheet of an excel file using interop library. I get the data just fine by using `Microsoft.Office.Interop.Excel` and then reading all data in the sheet. ``` Application ExcelApp = new Excel.Application(); Workbook excelWorkbook = ExcelApp2.Workbooks.Open(excelFileNamePath); _Worksheet excelWorksheet = excelWorkbook.Sheets[excelSheetName]; Range excelRange = excelWorksheet.UsedRange; //...for loops for reading data ExcelApp.Quit(); ``` But after my application terminates, I tried to edit my excel file and had some problems while opening. Apparently the excel process keeps on running in the background even though I called `ExcelApp.Quit()`. Is there a way to properly close the excel file that the application uses? I'm new to c# so i'm probably missing something here. Edit: Solved! Apparently there's a rule for com objects that discourages using 2 dots. ``` Excel.Application ExcelApp2 = new Excel.Application(); Excel.Workbooks excelWorkbooks = ExcelApp2.Workbooks; Excel.Workbook excelWorkbook = excelWorkbooks.Open(excelFileName); Excel._Worksheet excelWorksheet = excelWorkbook.Sheets[excelSheetName]; //... excelWorkbook.Close(); Marshal.ReleaseComObject(excelWorkbook); Marshal.ReleaseComObject(excelWorksheet); Marshal.ReleaseComObject(excelRange); ExcelApp2.Quit(); ```