CleanUp excel interop c#
本文关键字:interop excel CleanUp | 更新日期: 2023-09-27 18:27:09
我正在保存到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;
也许"sheet.Cells"是您必须发布的引用:
var cells = sheet.Cells;
...
Marshal.ReleaseComObject(cells);