$ cat "

Programmatically Add Conditional Formatting Formula to a Cell in Excel

"

The following snippet demonstrates how to add a conditional formatting rule to a cell. The rule specifies that the cell color is set to red if the contents of the cell has a length in characters longer than 3.

The snippet assumes an existing worksheet instance.

var range = (Excel.Range)worksheet.Cells[rowNo + 2, colNo + 1];

Excel.FormatCondition condition = (Excel.FormatCondition)range.FormatConditions.Add(
Microsoft.Office.Interop.Excel.XlFormatConditionType.xlExpression,
Type.Missing, "=LEN(R[0]C[0])>3", Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing);

condition.Interior.ColorIndex = 3; // Red

Written by Erik Öjebo 2009-04-15 14:30

    Comments