I am trying to work with Excel via COM interop. It works fine but Excel process is still hanging after DoStuff
method is finished (though it disappears when my program finishes).
If I remove the code that fills cell I do not have this issue.
using System;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel = Microsoft.Office.Interop.Excel;
namespace ConsoleApplication13
{
class Program
{
static void DoStuff()
{
string workbookPath = @"C:\....xlsx";
var excelApp = new Excel.Application();
excelApp.Visible = true;
var workbooks = excelApp.Workbooks;
var workbook = workbooks.Open(workbookPath,
0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "",
true, false, 0, true, false, false);
var workheets = workbook.Worksheets;
var mainWorksheet = (Excel.Worksheet) workheets.Item[1];
var cell = (Excel.Range)mainWorksheet.Cells[4, "U"];
cell.set_Value(Missing.Value, 99);
ReleaseObject(cell);
ReleaseObject(mainWorksheet);
ReleaseObject(workheets);
workbook.Close(false, Missing.Value, Missing.Value);
ReleaseObject(workbook);
ReleaseObject(workbooks);
excelApp.Quit();
ReleaseObject(excelApp);
}
private static void ReleaseObject(object obj)
{
if (obj != null && Marshal.IsComObject(obj))
{
Marshal.ReleaseComObject(obj);
}
}
static void Main(string[] args)
{
DoStuff();
Console.ReadKey();
}
}
}
I am trying to store all references to COM objects and release it as was suggested here How do I properly clean up Excel interop objects? but it doesn't work for Cells and I don't understand why.