1
使用Excel互操作,我試圖非常小心不隱式地創建任何對象(c.f. two dot rule),並確保一切都正確釋放。C# - Excel:單元格對象是一個COM對象嗎?
如果我從COM對象屬性創建一個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;
}
我想我只需要運行它並找出答案。爲了以防萬一,我會在finally {}塊中嵌入一個try-catch塊。 – CtrlDot 2010-08-21 03:57:11