使用C#在Excel中进行条件格式设置 [英] Conditional Formatting in Excel with C#
本文介绍了使用C#在Excel中进行条件格式设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果该值与另一列中的值不同,则需要对单元格的文本应用颜色.最好的方法是什么?我能想到的方法非常昂贵.
I need to apply a color to a cell's text if the value is not same as a value in another column. What would be the best approach for it ? The way I can think of is quite expensive.
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);
}
}
尝试如下条件格式,但徒劳无功.
Tried conditional formatting as below, but in vain.
FormatCondition cond = ColumnBRange.FormatConditions.Add(XlFormatConditionType.xlCellValue, XlFormatConditionOperator.xlNotEqual, ColumnARange);
cond.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
我正在使用VSTO,C#
I am using VSTO,C#
推荐答案
以下代码将条件格式添加到D1到E10的单元格范围中
The following code, adds a conditional formatting to the cell range of D1 to E10
它分别比较值D1 = E1或D2 = E2.您可以设置字体颜色,或在FormatCondition对象上填充颜色.
It compares the values D1 = E1 or D2 = E2 respectively. you can set the font color, or color fill on the FormatCondition Object.
FormatCondition format =(FormatCondition)( targetSheet.get_Range("D1:E10",
Type.Missing).FormatConditions.Add(XlFormatConditionType.xlExpression,
XlFormatConditionOperator.xlEqual,
"=$D1=$E1",
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing));
format.Font.Bold = true;
format.Font.Color = 0x000000FF;
这篇关于使用C#在Excel中进行条件格式设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文