【问题标题】:C# - Excel: Is the cell object a COM object?C# - Excel:单元格对象是 COM 对象吗?
【发布时间】:2014-10-02 22:44:36
【问题描述】:

使用 Excel 互操作时,我会非常小心,不要隐式创建任何对象(c.f. two dot rule)并确保所有内容都正确发布。

如果我从 COM 对象属性创建 System.Object,该对象是 COM 对象吗?为了澄清,以下面的 cell 对象为例。我需要打电话给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;
}

【问题讨论】:

    标签: c# excel


    【解决方案1】:

    我相信你确实必须释放它,因为我认为实际上没有任何像单元格对象这样的东西——一个单元格实际上是另一个 Range 对象,它恰好只覆盖一个单元格.

    【讨论】:

    • 我想我只需要运行它并找出答案。为了以防万一,我会在 finally{} 块中嵌入一个 try-catch 块。
    猜你喜欢
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-09
    • 2013-05-05
    • 2019-11-10
    • 2021-04-11
    • 2013-02-02
    • 2018-11-16
    相关资源
    最近更新 更多