Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Formatting in Excel with C#

Tags:

c#

excel

vsto

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#

like image 415
Cannon Avatar asked Apr 20 '12 04:04

Cannon


People also ask

What is C in Excel?

Usually, when we type (c) into a cell in Excel, it will be corrected to © automatically.

Are an example of conditional formats in Excel?

Understanding conditional formatting For example, a conditional formatting rule might be: If the value is less than $2000, color the cell red. By applying this rule, you'd be able to quickly see which cells contain values less than $2000.


3 Answers

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;
like image 101
Akanksha Gaur Avatar answered Sep 19 '22 16:09

Akanksha Gaur


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]

like image 24
joiharalds Avatar answered Sep 21 '22 16:09

joiharalds


Try this

FormatCondition cond = ColumnBRange.FormatConditions.Add(XlFormatConditionType.xlCellValue, XlFormatConditionOperator.xlNotEqual, "=$B1");
                cond.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red);
like image 23
Suresh Mahawar Avatar answered Sep 21 '22 16:09

Suresh Mahawar