簡體   English   中英

清理Excel Interop C#

[英]CleanUp excel interop c#

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM