2013-09-23 49 views
0

我有以下函数= AND(EXACT(B3; F3); EXACT(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); 
+0

允许@ chewmewaba4写他/她想要的。 – varocarbas

回答

0

您没有将颜色与给定规则(但与变量与条件格式不相关)关联。你也应该更好地依靠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); 
+0

感谢您的解决方案,但使用条件后它不能保存xls xlWorkBook.SaveAs(Environment.CurrentDirectory +“\\ Update_Report.xls”,Excel.XlFileFormat.xlWorkbookNormal,misValue,misValue,misValue,misValue,Excel.XlSaveAsAccessMode .xlExclusive,misValue,misValue,misValue,misValue,misValue); 猜测它的问题旧格式 – szabieable

+0

@SzabolcsSzrenko我打开一个xls文件,写我的代码和保存()这个文件,我没有任何问题。请记住,您问过为什么您的格式无法正常工作,现在这个工作正在进行中。如果您想查询更多问题(关于Excel自动化或其他),您应该发布一个新问题。我不介意帮助你一点,但从你所说的话看来,你需要的不仅仅是一些孤立的提示。我写的代码在给定范围之外没有任何影响(Cells [i,“M”])。你可能必须适应你的具体情况,但没有别的。 – varocarbas

+0

是的,你说得对。对不起,质量问题和感谢您的帮助! – szabieable

0

(道歉张贴此作为一个答案,但我没有足够的代表处添加评论还) 你有行:

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

而你的第三个参数“TRUE”通常代表公式。如果你想这个工作,你需要将它改为“= TRUE”。同样,你有“FALSE”,应该更新为“= FALSE”。

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

+0

你现在可以尝试写你的评论(我认为你可以发表评论,在有人写了一些东西后)。他不需要写“= True”。他的代码之所以不起作用,是因为我解释的原因:他并没有将颜色与条件联系起来(只对外部变量),因此不会发生颜色修改。而且他依赖于一个XlFormatConditionType,它至少在我的计算机上触发一个错误(逻辑的是xlCellValue)。请通过评论(你现在可以)写下你想要的任何内容,如果你认为不应该在这里,请删除这个答案。 – varocarbas

相关问题