Conditional Formatting in Excel with C#

不羁岁月 提交于 2020-01-22 20:15:35

问题


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);

I am using VSTO,C#


回答1:


The following code, adds a conditional formatting to the cell range of D1 to E10

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;



回答2:


Let's say you want to color cells B1:B10 if their values are not equal to those of A1:A10, i.e.

B1<>A1 results in B1 being colored, B2<>A2 results in B2 being colored etc.

Then you can do the following

Range columnBRange = (Range)oSheet.Range[oSheet.Cells[1,2], oSheet.Cells[10,2]];

Range columnARange = (Range)oSheet.Range[oSheet.Cells[1,1], oSheet.Cells[1,1]];

FormatCondition cond = (FormatCondition) ColumnBRange.FormatConditions.Add(XlFormatConditionType.xlCellValue, XlFormatConditionOperator.xlNotEqual, "="+ColumnARange.Address[false,true]);
cond.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); //Red letters
cond.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.LightYellow); //Light yellow cell background

Note that prepending "=" to ColumnARange.Address[false,true] is required because otherwise the Add method uses the Address as a literal string instead of a cell reference.

If you take look at the conditional formatting rule applied to the B1:B10 cells in the Excel sheet it will state Cell Value <> B1 for every cell in the range which is a bit confusing IMO but the formatting is applied correctly nonetheless.

For completeness: I'm using optional objects on Range.Address property like so Range.Address[isRowAbsolute,isColumnAbsolute]




回答3:


Try this

FormatCondition cond = ColumnBRange.FormatConditions.Add(XlFormatConditionType.xlCellValue, XlFormatConditionOperator.xlNotEqual, "=$B1");
                cond.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);


来源:https://stackoverflow.com/questions/10240132/conditional-formatting-in-excel-with-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!