C# – Excel:单元格对象是一个COM对象吗?

使用Excel互操作,我试图非常小心不隐式地创build任何对象(比较两点规则 ),并确保一切正常释放。

如果我从一个COM对象属性创build一个System.Object,该对象是一个COM对象吗? 为了澄清,以下面的单元格对象的情况。 我是否需要调用Marshal.ReleaseComObject(cell)或不?

 public static float RangeLeft(Excel.Worksheet sheet, int row, int col) { float returnValue; object cell = null; Excel.Range range = null; try { cell = sheet.Cells[row, col]; range = sheet.get_Range(cell, Type.Missing); returnValue = (float)range.Left; } catch (COMException comex) { Debug.WriteLine(comex.Message); throw; } finally { if (cell != null) { Marshal.ReleaseComObject(cell); cell = null; } if (range != null) { Marshal.ReleaseComObject(range); range = null; } } return returnValue; } 

我相信你不得不释放它,因为我不认为实际上有任何一个单元格对象 – 一个单元格实际上是另一个Range对象,它恰好只覆盖了一个单元格。