0

我正在保存指向 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;
4

1 回答 1

0

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

var cells = sheet.Cells;

...

Marshal.ReleaseComObject(cells);
于 2013-08-11T16:06:26.177 回答