C#excel格式条件

时间:2014-02-03 15:55:08

标签: c# office-interop

我正在尝试在C#中使用Excel条件格式。以下是代码:

Excel.Range rangeFormat = ws.get_Range("F1", "F1");
Excel.FormatConditions fcs = range.FormatConditions;
Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add
    (Excel.XlFormatConditionType.xlExpression, Type.Missing, "=IF($F$1) >= 10", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
Excel.Interior interior = fc.Interior;
interior.Color = ColorTranslator.ToOle(Color.Red);
interior = null;
fc = null;
fcs = null;

但是,我得到ParameterCountException,但我确信我已经给它了正确数量的参数。

1 个答案:

答案 0 :(得分:0)

MSDN建议Excel.FormatCondition.Add最多需要4个参数

FormatCondition Add(
    [In] XlFormatConditionType Type, 
    [In, Optional] object Operator, 
    [In, Optional] object Formula1, 
    [In, Optional] object Formula2
);

您可以尝试输入:

Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add(
    Type: Excel.XlFormatConditionType.xlExpression,
    Formula1: "=IF($F$1) >= 10"
);

虽然您使用的公式看起来也可能不正确。我认为你probalby想=$F$1 >= 10,虽然在这种情况下你也应该能够使用:

Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add(
    Type: Excel.XlFormatConditionType.xlExpression,
    Operator: Excel.XlFormatConditionOperator.xlGreaterEqual
    Formula1: "10"
);