使用C#在Excel中进行条件格式化

如果值与另一列中的值不同,我需要将颜色应用于单元格的文本.最好的方法是什么?我能想到的方式非常昂贵.
for (int i = 0; i < Columnarange.Cells.Count; i++)
                    {
                        if (Columnarange.Cells[i,1] != ColumnBRange.Cells[i,1])
                        {
                            Range currCell = ColumnBRange.Cells[i,1];
                            currCell.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
                        }
                    }

尝试条件格式如下,但徒劳无功.

FormatCondition cond = ColumnBRange.FormatConditions.Add(XlFormatConditionType.xlCellValue,XlFormatConditionoperator.xlNotEqual,Columnarange);
                cond.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);

我正在使用VSTO,C#

解决方法

以下代码将条件格式添加到D1到E10的单元格区域

它分别比较值D1 = E1或D2 = E2.您可以在FormatCondition对象上设置字体颜色或颜色填充.

FormatCondition format =(FormatCondition)( targetSheet.get_Range("D1:E10",Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression,XlFormatConditionoperator.xlEqual,"=$D1=$E1",Type.Missing,Type.Missing));

            format.Font.Bold = true;
            format.Font.Color = 0x000000FF;

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...