簡體   English   中英

C#-Excel:單元格對象是COM對象嗎?

[英]C# - Excel: Is the cell object a COM object?

使用Excel interop時,我要非常小心,不要隱式創建任何對象(參見2點規則 ),並確保所有內容均已正確發布。

如果我從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;
}

我相信您必須釋放它,因為我認為實際上沒有像單元格對象之類的東西-單元格實際上是另一個Range對象,它恰好覆蓋了一個單元格。

暫無
暫無

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

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