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

C# 突出显示规则文本包含的单元格,c#,excel,interop,C#,Excel,Interop,我有以下函数=AND(精确的(B3;F3);精确的(F3;J3)),它返回TRUE或FALSE。 我想创建一个单元格规则,将假值涂成红色,将真值涂成绿色。 尝试使用以下代码,但不起作用,我做错了什么 Excel.FormatConditions fcs = xlWorkSheet.Cells[i,"M"].FormatConditions; Excel.FormatCondition fc = (Excel.FormatCondition)fcs.A

我有以下函数=AND(精确的(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建议。

以允许@chewmewaba4写他/她想要的东西。您可以尝试现在就写评论(我认为您可以在有人写了东西之后发表评论)。他不需要写“=真”。他的代码不起作用的原因是因为我解释过的原因:他没有将颜色与条件相关联(只是与外部变量相关联),因此无法进行颜色修改。他依赖于一个XlFormatConditionType,至少在我的计算机上,它会触发一个错误(逻辑类型是xlCellValue)。请通过评论写下您想要的任何内容(您现在可以),如果您认为不应该在这里,请删除此答案。感谢您提供的解决方案,但在使用该条件后,它无法保存xls xlWorkBook.SaveAs(Environment.CurrentDirectory+“\\Update\u Report.xls”,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode.xlExclusive,misValue,misValue,misValue,misValue,misValue);我猜这是老问题了format@SzabolcsSzrenko我打开一个xls文件,编写代码并保存()这个文件,我没有任何问题。请记住,您曾经问过为什么您的格式设置不起作用,而现在它起作用了。如果你想询问更多的问题(关于Excel自动化或其他任何事情),你应该发布一个新问题。我不介意帮你一点忙,但从你所说的来看,似乎你需要的不仅仅是几个单独的提示。我编写的代码在给定范围(单元格[I,“M”])之外没有任何效果。你可能需要根据自己的具体情况进行调整,但除此之外别无选择。是的,你说得对。很抱歉出现了大量问题,ofc感谢您的帮助@SzabolcsSzrenko没问题。我本打算开始回答,但在回答的过程中,我意识到我需要更多的信息(=更好地回答一个新问题)。