[英]Marshal.ReleaseComObject(…) issues
完成后,我使用上述調用來釋放一些Excel對象。 之后是否需要將引用設置為null(如以下代碼所示)?
var dateCol = sheet1.get_Range("C4", "C" + rowOffset);
dateCol.NumberFormat = "Text";
Marshal.ReleaseComObject(dateCol);
dateCol = null;
這是我一直使用的並且效果很好
using Excel = Microsoft.Office.Interop.Excel;
Excel.ApplicationClass _Excel;
Excel.Workbook WB;
Excel.Worksheet WS;
try
{
_Excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
WB = _Excel.Workbooks.Open("FILENAME",
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
//do something
}
catch (Exception ex)
{
WB.Close(false, Type.Missing, Type.Missing);
throw;
}
finally
{
GC.Collect();
GC.WaitForPendingFinalizers();
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(WB);
System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_Excel);
}
做一個
System.Runtime.InteropServices.Marshal.FinalReleaseComObject
在所有引用的Excel對象上
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.