加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 08:27:44 所属栏目:百科 来源:网络整理
导读:如果值与另一列中的值不同,我需要将颜色应用于单元格的文本.最好的方法是什么?我能想到的方式非常昂贵. for (int i = 0; i ColumnARange.Cells.Count; i++) { if (ColumnARange.Cells[i,1] != ColumnBRange.Cells[i,1]) { Range currCell = ColumnBRange.Ce
如果值与另一列中的值不同,我需要将颜色应用于单元格的文本.最好的方法是什么?我能想到的方式非常昂贵.
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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读