How to Apply Conditional Formatting to Cells

Conditional formatting is a feature in spreadsheet and data processing software that allows you to apply specific formatting styles or rules to cells or data based on certain conditions or criteria. It enables you to visually highlight or emphasize data that meets particular conditions, making it easier to analyze and understand data in a spreadsheet or table.

Easily Add, Retrieve, and Remove Conditional Formatting with IronXL. When adding conditional formatting with styling, you can make font and size adjustments, set borders and alignment, and define background patterns and colors


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Add Conditional Formatting Example

Conditional formatting consists of rules and styles that are applied when a cell meets the specified rule criteria. The styles can include font and size adjustments, borders and alignment settings, as well as background patterns and colors.

To define a rule, use the CreateConditionalFormattingRule method provided by IronXL. Assign the object returned by this method to a variable, and use it to apply the desired styling. Finally, utilize the AddConditionalFormatting method, providing both the created rule and the cell range to which it should be applied.

:path=/static-assets/excel/content-code-examples/how-to/conditional-formatting-add.cs
using IronXL;
using IronXL.Formatting.Enums;

WorkBook workBook = WorkBook.Load("sample.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Create conditional formatting rule
var rule = workSheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8");

// Set style options
rule.PatternFormatting.BackgroundColor = "#54BDD9";

// Add conditional formatting rule
workSheet.ConditionalFormatting.AddConditionalFormatting("A1:A10", rule);

workBook.SaveAs("addConditionalFormatting.xlsx");
Imports IronXL
Imports IronXL.Formatting.Enums

Private workBook As WorkBook = WorkBook.Load("sample.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Create conditional formatting rule
Private rule = workSheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8")

' Set style options
rule.PatternFormatting.BackgroundColor = "#54BDD9"

' Add conditional formatting rule
workSheet.ConditionalFormatting.AddConditionalFormatting("A1:A10", rule)

workBook.SaveAs("addConditionalFormatting.xlsx")
$vbLabelText   $csharpLabel
Before
After

Below are all the available rules:

  • NoComparison: The default value.
  • Between: 'Between' operator
  • NotBetween: 'Not between' operator
  • Equal: 'Equal to' operator
  • NotEqual: 'Not equal to' operator
  • GreaterThan: 'Greater than' operator
  • LessThan: 'Less than' operator
  • GreaterThanOrEqual: 'Greater than or equal to' operator
  • LessThanOrEqual: 'Less than or equal to' operator

Retrieve Conditional Formatting Example

To retrieve a conditional formatting rule, use the GetConditionalFormattingAt method. The rule object returned may contain multiple rules; use the GetRule method to access a specific one. Most properties of a retrieved rule cannot be modified, yet you can adjust the BackgroundColor via the PatternFormatting property. The following code demonstrates this:

:path=/static-assets/excel/content-code-examples/how-to/conditional-formatting-retrieve.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addConditionalFormatting.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Create conditional formatting rule
var ruleCollection = workSheet.ConditionalFormatting.GetConditionalFormattingAt(0);
var rule = ruleCollection.GetRule(0);

// Edit styling
rule.PatternFormatting.BackgroundColor = "#B6CFB6";

workBook.SaveAs("editedConditionalFormatting.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addConditionalFormatting.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Create conditional formatting rule
Private ruleCollection = workSheet.ConditionalFormatting.GetConditionalFormattingAt(0)
Private rule = ruleCollection.GetRule(0)

' Edit styling
rule.PatternFormatting.BackgroundColor = "#B6CFB6"

workBook.SaveAs("editedConditionalFormatting.xlsx")
$vbLabelText   $csharpLabel
Before
After

Remove Conditional Formatting Example

To remove a conditional formatting rule, use the RemoveConditionalFormatting method. Pass the index of the targeted rule to this method.

:path=/static-assets/excel/content-code-examples/how-to/conditional-formatting-remove.cs
using IronXL;

WorkBook workBook = WorkBook.Load("addConditionalFormatting.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Remove conditional formatting rule
workSheet.ConditionalFormatting.RemoveConditionalFormatting(0);

workBook.SaveAs("removedConditionalFormatting.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("addConditionalFormatting.xlsx")
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Remove conditional formatting rule
workSheet.ConditionalFormatting.RemoveConditionalFormatting(0)

workBook.SaveAs("removedConditionalFormatting.xlsx")
$vbLabelText   $csharpLabel

Frequently Asked Questions

What is conditional formatting in Excel?

Conditional formatting in Excel is a feature that allows you to apply specific formatting styles to cells based on defined criteria. This helps in visually highlighting or emphasizing data for easier analysis and understanding.

How do I apply conditional formatting to cells using C#?

To apply conditional formatting to cells using C#, use the IronXL library. Start by downloading it from NuGet. Then, create a rule with the CreateConditionalFormattingRule method and apply it with the AddConditionalFormatting method.

Can I retrieve existing conditional formatting rules with IronXL?

Yes, you can retrieve existing conditional formatting rules using the GetConditionalFormattingAt method provided by IronXL. This allows you to access and manage rules applied to cells in an Excel spreadsheet.

What operators can I use for conditional formatting in IronXL?

IronXL supports several operators for conditional formatting, including NoComparison, Between, NotBetween, Equal, NotEqual, GreaterThan, LessThan, GreaterThanOrEqual, and LessThanOrEqual.

Is it possible to modify a conditional formatting rule after retrieving it?

While most properties of a retrieved conditional formatting rule cannot be modified, you can adjust the BackgroundColor using the PatternFormatting property in IronXL.

How can I remove a conditional formatting rule from a cell range?

To remove a conditional formatting rule from a cell range, use the RemoveConditionalFormatting method in IronXL and provide the index of the rule you wish to delete.

What styling options are available with conditional formatting in IronXL?

With IronXL, you can apply a variety of styles in conditional formatting such as font and size adjustments, borders, alignment settings, and background patterns and colors.

What is the first step to using IronXL for conditional formatting?

The first step is to download the IronXL.Excel library from NuGet, enabling you to implement conditional formatting in Excel spreadsheets using C#.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.