微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用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;

原文地址:https://www.jb51.cc/csharp/243657.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐