I am using the below methods. After this code block executes, I find that Microsoft Excel is still running in the background via checking Task Manager.
Edit: Exactly one Microsoft Excel background process remains live. I am just adding this because when I first began working with Excel and Interop COM objects, sometimes many processes would stay running after the functions finished executing.
How do I ensure that Excel is not running in the background after my current function is executed?
I declare these variables with global scope because several functions end up needing them:
private Excel.Application xls = null;
private Excel.Workbooks workBooks = null;
private Excel.Workbook workBook = null;
private Excel.Sheets sheets = null;
private Excel.Worksheet workSheet = null;
This method is for checking if a sheet exists in a given workbook:
private bool sheetExists(string searchString)
{
xls = new Excel.Application();
workBooks = xls.Workbooks;
workBook = workBooks.Open(workbookPath); // <- Where the workbook is saved
sheets = workBook.Sheets;
bool isFound = false;
foreach (Excel.Worksheet sheet in sheets)
{
// Check the name of the current sheet
if (sheet.Name == searchString)
{
Marshal.ReleaseComObject(sheet);
isFound = true;
break;
}
Marshal.ReleaseComObject(sheet);
}
cleanUp(true, xls, workBooks, workBook, sheets);
return isFound;
}
This function is for releasing the COM objects:
private void cleanUp(bool saveTrueFalse, params object[] comObjects)
{
workBook.Close(saveTrueFalse);
xls.Application.Quit();
xls.Quit();
foreach (object o in comObjects)
{
Marshal.ReleaseComObject(o);
}
workSheet = null;
sheets = null;
workBook = null;
workBooks = null;
xls = null;
GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
GC.WaitForPendingFinalizers();
}