Class FormatCondition

FormatCondition class

Represents conditional formatting condition.

public class FormatCondition

Properties

NameDescription
AboveAverage { get; }Get the conditional formatting’s “AboveAverage” instance. The default instance’s rule highlights cells that are above the average for all values in the range. Valid only for type = AboveAverage.
ColorScale { get; }Get the conditional formatting’s “ColorScale” instance. The default instance is a “green-yellow-red” 3ColorScale . Valid only for type = ColorScale.
DataBar { get; }Get the conditional formatting’s “DataBar” instance. The default instance’s color is blue. Valid only for type is DataBar.
Formula1 { get; set; }Gets and sets the value or expression associated with conditional formatting.
Formula2 { get; set; }Gets and sets the value or expression associated with conditional formatting.
IconSet { get; }Get the conditional formatting’s “IconSet” instance. The default instance’s IconSetType is TrafficLights31. Valid only for type = IconSet.
Operator { get; set; }Gets and sets the conditional format operator type.
Priority { get; set; }The priority of this conditional formatting rule. This value is used to determine which format should be evaluated and rendered. Lower numeric values are higher priority than higher numeric values, where ‘1’ is the highest priority.
StopIfTrue { get; set; }True, no rules with lower priority may be applied over this rule, when this rule evaluates to true. Only applies for Excel 2007;
Style { get; set; }Gets or setts style of conditional formatted cell ranges.
Text { get; set; }The text value in a “text contains” conditional formatting rule. Valid only for type = containsText, notContainsText, beginsWith and endsWith. The default value is null.
TimePeriod { get; set; }The applicable time period in a “date occurring…” conditional formatting rule. Valid only for type = timePeriod. The default value is TimePeriodType.Today.
Top10 { get; }Get the conditional formatting’s “Top10” instance. The default instance’s rule highlights cells whose values fall in the top 10 bracket. Valid only for type is Top10.
Type { get; set; }Gets and sets whether the conditional format Type.

Methods

NameDescription
GetFormula1(bool, bool)Gets the value or expression associated with this format condition.
GetFormula1(int, int)Gets the formula of the conditional formatting of the cell.
GetFormula1(bool, bool, int, int)Gets the value or expression of the conditional formatting of the cell.
GetFormula2(bool, bool)Gets the value or expression associated with this format condition.
GetFormula2(int, int)Gets the formula of the conditional formatting of the cell.
GetFormula2(bool, bool, int, int)Gets the value or expression of the conditional formatting of the cell.
SetFormula1(string, bool, bool)Sets the value or expression associated with this format condition.
SetFormula2(string, bool, bool)Sets the value or expression associated with this format condition.
SetFormulas(string, string, bool, bool)Sets the value or expression associated with this format condition.

Examples


[C#]
//Instantiating a Workbook object
Workbook workbook = new Workbook();
Worksheet sheet = workbook.Worksheets[0];
 
//Adds an empty conditional formatting
int index = sheet.ConditionalFormattings.Add();
FormatConditionCollection fcs = sheet.ConditionalFormattings[index];
 
//Sets the conditional format range.
CellArea ca = new CellArea();
ca.StartRow = 0;
ca.EndRow = 0;
ca.StartColumn = 0;
ca.EndColumn = 0;
fcs.AddArea(ca);
 
ca = new CellArea();
ca.StartRow = 1;
ca.EndRow = 1;
ca.StartColumn = 1;
ca.EndColumn = 1;
fcs.AddArea(ca);
 
//Adds condition.
int conditionIndex = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100");
 
//Adds condition.
int conditionIndex2 = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100");
 
//Sets the background color.
FormatCondition fc = fcs[conditionIndex];
fc.Style.BackgroundColor = Color.Red;
 
//Saving the Excel file
workbook.Save("output.xls");

[VB.NET]

'Instantiating a Workbook object
Dim workbook As Workbook = New Workbook()
Dim sheet As Worksheet = workbook.Worksheets(0)
 
' Adds an empty conditional formatting
Dim index As Integer = sheet.ConditionalFormattings.Add()
Dim fcs As FormatConditionCollection = sheet.ConditionalFormattings(index)
 
'Sets the conditional format range.
Dim ca As CellArea = New CellArea()
ca.StartRow = 0
ca.EndRow = 0
ca.StartColumn = 0
ca.EndColumn = 0
fcs.AddArea(ca)
ca = New CellArea()
ca.StartRow = 1
ca.EndRow = 1
ca.StartColumn = 1
ca.EndColumn = 1
fcs.AddArea(ca)
 
'Adds condition.
Dim conditionIndex As Integer = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "=A2", "100")
 
'Adds condition.
Dim conditionIndex2 As Integer = fcs.AddCondition(FormatConditionType.CellValue, OperatorType.Between, "50", "100")
 
'Sets the background color.
Dim fc As FormatCondition = fcs(conditionIndex)
fc.Style.BackgroundColor = Color.Red
 
'Saving the Excel file
workbook.Save("output.xls")

See Also