'Close Exel Task in Taskmanager
I got a problem with accessing an excel file and closing it afterwards completely. I read a lot of forum entrys and articles about it and my code is a lot better than at the start. So currently i I am activating line after line and I am checking if the closing is still possible. The codeline which is causing trouble is marked below. With this active, the excel task is still present in my task manager. I think it has something to do with the reading of the cell value, but I dont understand whats the problem. The reading has no problem, only the task will be still open in my taskmanager.
One more thing: The Function is called inside a Backgroundworker, if this is important.
Thank you for all your help and sorry for my formulation! If something is not clear, please ask and I will try to explain it better
public string CallExcel(string releaseDescription = "", string releaseCntNr = "")
{
Excel.Application ExcelApp = null;
Excel.Workbooks books = null;
Excel.Workbook workbook = null;
Excel.Sheets sheets = null;
Excel.Worksheet worksheet = null;
Excel.Range range = null;
Excel.Range testrange = null;
Excel.Range rows = null;
Excel.Range cells = null;
try
{
ExcelApp = new Excel.Application();
ExcelApp.DisplayAlerts = false;
books = ExcelApp.Workbooks;
workbook = books.Open("C:\\temp\\Tooltest\\BB-No_for_ACC_STIL_routines.xlsx");
sheets = workbook.Sheets;
worksheet = sheets[Math.Min(0x00000001, sheets.Count)];
range = worksheet.UsedRange;
rows = range.Rows;
cells = range.Cells;
//PROBLEMATIC LINE:
var testvar = cells[0x0000006c, 0x00000002].Value;
//for (uint iRow = 0x0000006a; iRow <= rows.Count; iRow++)
//{ blabla}
}
catch (Exception)
{
throw;
}
finally
{
//Release in opposite order
if (cells != null) Marshal.ReleaseComObject(cells); //Not sure if neccessary
if (rows != null) Marshal.ReleaseComObject(rows); //Not sure if neccessary
if (testrange != null) Marshal.ReleaseComObject(testrange);
if (range != null) Marshal.ReleaseComObject(range);
if (worksheet != null) Marshal.ReleaseComObject(worksheet);
if (sheets != null) Marshal.ReleaseComObject(sheets);
if (workbook != null)
{
workbook.Close(true, Type.Missing, Type.Missing);
Marshal.ReleaseComObject(workbook);
}
if (books != null) Marshal.ReleaseComObject(books);
if (ExcelApp != null)
{
ExcelApp.Quit();
Marshal.ReleaseComObject(ExcelApp);
}
}
// Cleanup
GC.Collect();
GC.WaitForPendingFinalizers();
return "something";
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
