突出显示单元格规则文本包含



我有以下函数 =AND(EXACT(B3;F3);精确(F3;J3)) 返回 TRUE 或 FALSE。我想创建一个单元格规则,用于将红色着色为假值,绿色为真值着色。尝试使用以下代码,但不起作用,我做错了什么?

   Excel.FormatConditions fcs = xlWorkSheet.Cells[i,"M"].FormatConditions;
                    Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add(Excel.XlFormatConditionType.xlTextString, Excel.XlFormatConditionOperator.xlEqual, "TRUE");
                    Excel.Interior interior = fc.Interior;
                    interior.Color = ColorTranslator.ToOle(Color.LightGreen);
                    Excel.Font font = fc.Font;
                    font.Color = ColorTranslator.ToOle(Color.ForestGreen);
                    fc = (Excel.FormatCondition)fcs.Add(Excel.XlFormatConditionType.xlTextString, Excel.XlFormatConditionOperator.xlEqual, "FALSE");
                    interior.Color = ColorTranslator.ToOle(Color.LightSalmon);
                    font.Color = ColorTranslator.ToOle(Color.Red);

您没有将颜色与给定规则相关联(而是与与条件格式无关的变量)相关联。此外,您最好依靠xlCellValue。此代码提供您所追求的内容:

Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "TRUE", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
fc.Interior.Color = ColorTranslator.ToOle(Color.LightGreen);
fc.Font.Color = ColorTranslator.ToOle(Color.ForestGreen);
fc = (Excel.FormatCondition)fcs.Add(Excel.XlFormatConditionType.xlCellValue, Excel.XlFormatConditionOperator.xlEqual, "FALSE", Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
fc.Interior.Color = ColorTranslator.ToOle(Color.LightSalmon);
fc.Font.Color = ColorTranslator.ToOle(Color.Red);
(很

抱歉将此作为答案发布,但我没有足够的代表来添加评论)你有行:

Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add(Excel.XlFormatConditionType.xlTextString, Excel.XlFormatConditionOperator.xlEqual, "TRUE");

您的第三个参数"TRUE"通常表示公式。 如果您希望此功能正常工作,则需要将其更改为"=TRUE"。 同样,你有"FALSE",应该更新为"=FALSE"。

您还需要包含上述@varocarbas建议。

最新更新