0

I'm saving links to excel file, but once i saved it the excel process is still runnning. How i should relase this excel com object?

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook book = app.Workbooks.Add(1);
Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.Worksheets[1];

for (int i = 0; i < Links.Count; i++)
{
   sheet.Cells[i + 1, 1] = Links[i];
}

book.SaveAs("test.xlsx");

Marshal.ReleaseComObject(sheet);
sheet = null;
book.Close(true, null, null);
Marshal.ReleaseComObject(book);
book = null;
app.Quit();
Marshal.ReleaseComObject(app);
app = null;
Yozer
  • 648
  • 1
  • 11
  • 26
  • 3
    answered here: http://stackoverflow.com/questions/158706/how-to-properly-clean-up-excel-interop-objects – Keith Payne Aug 11 '13 at 15:37
  • Look at my code, i'm releasing every object. – Yozer Aug 11 '13 at 15:41
  • 1
    @KeithPayne is referring to the undeclared Workbooks and Worksheets collections and the "two dots“ issue. – Doug Glancy Aug 11 '13 at 16:18
  • There's a Range interface reference being used in this code that you can never get to, it is entirely invisible. Using ReleaseComObject() is just plain wrong. Backgrounder [is here](http://stackoverflow.com/questions/17130382/understanding-garbage-collection-in-net/17131389#17131389) – Hans Passant Aug 11 '13 at 16:59
  • @Keith Payne - right sorry, stupid me. I didn't read a whole post. Problem solved. – Yozer Aug 11 '13 at 17:26

1 Answers1

0

Maybe "sheet.Cells" is a reference that you have to release:

var cells = sheet.Cells;

...

Marshal.ReleaseComObject(cells);
Alessandro D'Andria
  • 8,663
  • 2
  • 36
  • 32