【问题标题】:Excel Interop: Range.FormatConditions.Add throws MissingMethodExceptionExcel 互操作: Range.FormatConditions.Add 抛出 MissingMethodException
【发布时间】:2009-03-13 22:20:33
【问题描述】:

我正在编写一个应用程序,它使用 Microsoft.Office.Interop.Excel 程序集从 Excel 电子表格导出/导入数据。一切都很好(除了基于 1 的索引和所有这些可选参数!),直到我尝试使用条件格式。当我调用 Range.FormatConditions.Add 时,我得到一个 MissingMethodException 告诉我不存在这样的方法。这在 Vista 和 XP 中都会发生。

下面是生成异常的代码示例:

//1. Add a reference to Microsoft.Office.Interop.Excel (version 11.0.0.0)
//2. Compile and run the following code:

using Microsoft.Office.Interop.Excel;

class Program
{
    static void Main(string[] args)
    {
        Application app = new Application();
        Workbook workbook = app.Workbooks[1];
        Worksheet worksheet = (Worksheet)workbook.Worksheets[1];
        Range range = worksheet.get_Range("A1", "A5");
        FormatCondition condition = range.FormatConditions.Add(
            XlFormatConditionType.xlCellValue, 
            XlFormatConditionOperator.xlBetween, 
            100, 
            200);
    }
}

【问题讨论】:

    标签: .net excel com-interop conditional-formatting


    【解决方案1】:

    【讨论】:

    • 谢谢!该链接解决了问题。我能够使用反射来调用该方法,现在一切正常!
    【解决方案2】:

    FormatConditions 接口的 Add() 方法有 8 个参数,而不是 4 个。AFAIK 在 Excel.Extensions 命名空间中没有此类型的扩展方法。您是在上面的示例中使用了扩展方法还是...?

    【讨论】:

    • 我最近没有使用 Excel 互操作,但 IIRC 的 Add 的方法签名在 Excel 互操作库的版本 12 中更改为 8 个参数。 Office 2007 允许通过版本 11 进行访问。在版本 11 中,签名是 4 个参数。针对版本 11 进行编译(为了向后兼容)会强制您使用 4 个参数,但实际上在运行时不起作用。那是我遇到的错误。使用 Gavin Miller 提供的链接所示的反射解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多