CleanUp excel interop c#

时间:2013-08-11 15:34:18

标签: c# excel com interop

我保存了到excel文件的链接,但是一旦我保存了它,excel进程仍在运行。 我该如何重新发布这个excel com对象?

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;

1 个答案:

答案 0 :(得分:0)

也许“sheet.Cells”是您必须发布的参考:

var cells = sheet.Cells;

...

Marshal.ReleaseComObject(cells);