I need to create report in xlsx format. After the report has been created and file saved I am releasing resources but process is not disappearing from Task Manager. Here is my method.
excelApplication = new Excel.Application();
wBooks = excelApplication.Workbooks;
wBook = wBooks.Add(Excel.XlSheetType.xlWorksheet);
sheet = (Excel.Worksheet)excelApplication.ActiveSheet;
for (int j = 0; j < outDistribution.Count; j++)
{
var sh = (Excel.Range)sheet.Cells[j + 1];
sh.Value2 = outDistribution[j];
Marshal.ReleaseComObject(sh);
}
SaveFile(wBook);
}
catch (Exception ex)
{
}
finally
{
Helper.ReleaseCOMObject(sheet);
Helper.ReleaseCOMObject(wBook);
Helper.ReleaseCOMObject(wBooks);
Helper.ReleaseCOMObject(excelApplication);
}
ReleaseCOMObject method:
public void ReleaseCOMObject(object obj)
{
if (obj is Excel.Worksheet)
{
Marshal.FinalReleaseComObject(((Excel.Worksheet)obj).Cells);
Marshal.ReleaseComObject(obj);
obj = null;
}
if (obj is Excel.Workbook)
{
((Excel.Workbook)obj).Close(false);
Marshal.ReleaseComObject(obj);
obj = null;
}
if (obj is Excel.Workbooks)
{
((Excel.Workbooks)obj).Close();
Marshal.ReleaseComObject(obj);
obj = null;
}
if (obj is Excel.Application)
{
((Excel.Application)obj).Quit();
Marshal.ReleaseComObject(obj);
obj = null;
}
}
I have rad a lot of posts on this resource and others but nothing work for me.