Marshal.ReleaseComObject(…)问题

我用完所有的调用来释放一些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对象上