0

我正在一个项目中工作,我必须生成一个带有格式条件的 Excel。我能够使用等于值(例如:B2 =“正确值”)来做到这一点,但在某些情况下,我必须检查该值是否在两个值之间(例如:“2 < B2 < 4”)我有这个代码:

FormatCondition[] format = new FormatCondition[datos.Length];
            Microsoft.Office.Interop.Excel.Range[] cells = new Range[datos.Length];
            for (int i = 1; i < datos.Length; i++)
            {
                string condition = "";
                if (qTipo[i - 1] == "Num" && qMax[i - 1] != "" && qMin[i - 1] != "")
                { // DOES NOT WORK
                    condition = $"=Y(B{i + 1}<{qMax[i - 1]};B{i + 1}>{qMin[i - 1]})"; // THIS RETURNS "=Y(B4<6;B4>4)"
                    format[i] = (FormatCondition)(xlWorksheet.get_Range($"B{i + 1}",
                    Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression, Type.Missing,
                    condition));
                }
                else
                {   //WORKS
                    condition = $"=B{i + 1}=\"{qCorrecto[i - 1]}\""; // THIS RETURNS "=B2="Correct Value""
                    format[i] = (FormatCondition)(xlWorksheet.get_Range($"B{i + 1}",
                    Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression, Type.Missing,
                    condition));
                }
                format[i].Interior.Color = 0x0000FF00;
                datos[i] = new String[2];
                datos[i][0] = qPregunta[i - 1];
                datos[i][1] = "";
            }

当我执行此代码时,它在 Add 语句上给了我“无效的参数异常”。

谢谢,米格尔。

4

1 回答 1

0

感谢我的超级智能,我能够解决这个问题。我需要的代码是:

format[i] = (FormatCondition)(xlWorksheet.get_Range($"B{i + 1}", Type.Missing).FormatConditions.Add(XlFormatConditionType.xlC‌​ellValue, XlFormatConditionOperator.xlBetween, $"={qMax[i - 1]}", $"={qMin[i - 1]}"));

于 2017-06-07T10:40:29.423 回答