使用VS2010和Excel 2010访问FormatCondition的异常

时间:2011-10-16 02:17:41

标签: c# visual-studio-2010 excel excel-interop

当我尝试从FormatConditions集合中访问FormatCondition项时,我收到以下异常。

  

无法将“System .__ ComObject”类型的COM对象强制转换为接口类型“Microsoft.Office.Interop.Excel.FormatCondition”。此操作失败,因为由于以下错误,对IID为“{00024425-0000-0000-C000-000000000046}”的接口的COM组件的QueryInterface调用失败:不支持此类接口(HRESULT异常:0x80004002(E_NOINTERFACE))

我正在使用VS2010 SP1,C#,Excel 2010 32位

到目前为止,我创建了一个Excel电子表格,并向条件B3添加了条件格式。

在代码I中:

  1. 打开电子表格。
  2. get cell b3
  3. 验证FormatConditions.Count是否返回1.
  4. 当我尝试以下操作时,我得到了上述异常:

    fmtCond = (Excel.FormatCondition) testCell.FormatConditions.Item(1);
    

    在另一台PC上使用VS2005和Excel 2007构建时,我正在使用的代码似乎工作正常。

1 个答案:

答案 0 :(得分:0)

我没有尝试同时获取所有格式条件,而是在单独设置属性时发现它有效:

DestRange.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, SourceRange.FormatConditions(1).Operator, SourceRange.FormatConditions(1).Formula1)

DestRange.FormatConditions(1).Interior.ColorIndex = SourceRange.FormatConditions(1).Interior.ColorIndex

其中DestRangeSourceRange分别是您要将属性传输到的区域和原始范围。如果您已指定FormatConditions,则可能需要获取{{1}}的其他属性。

相关问题