使用VS2010和Excel 2010访问FormatConditionexception

当我尝试访问FormatConditions集合中的FormatCondition项目时,出现以下exception。

无法将“System .__ ComObject”types的COM对象转换为接口types“Microsoft.Office.Interop.Excel.FormatCondition”。 此操作失败,因为IIC“{00024425-0000-0000-C000-000000000046}”接口的COM组件上的QueryInterface调用失败,原因是出现以下错误:没有此类接口支持(exception来自HRESULT:0x80004002(E_NOINTERFACE)) 。

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

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

在代码I中:

  1. 打开电子表格。
  2. 获得细胞b3
  3. validationFormatConditions.Count返回1。

我遇到以上exception,当我尝试以下:

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

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

而不是试图抓住所有的格式条件,我发现它适用于单独设置属性:

 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您可能需要获取其他属性。