如何对单元格应用条件格式化

How to Apply Conditional Formatting to Cells

This article was translated from English: Does it need improvement?
Translated
View the article in English

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

Quickstart: Add a ‘Less Than’ Formatting Rule Effortlessly

Here’s how to get started fast with IronXL: create a conditional formatting rule using just one line and then instantly apply it to a range of cells. No complex setup—just define your condition and style, and IronXL handles the rest with ease.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    var rule = workSheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.LessThan, "8"); workSheet.ConditionalFormatting.AddConditionalFormatting("A1:A10", rule);
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


Get started with IronXL

今天在您的项目中使用 IronXL,免费试用。

第一步:
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

常见问题解答

什么是Excel中的条件格式?

Excel中的条件格式是一项功能,可以根据定义的标准将特定的格式样式应用于单元格。这有助于通过视觉强调或突出数据,便于更容易的分析和理解。

如何使用C#将条件格式应用于单元格?

要使用C#将条件格式应用于单元格,请使用IronXL库。首先从NuGet下载它。然后,使用CreateConditionalFormattingRule方法创建规则,并使用AddConditionalFormatting方法应用它。

我可以使用IronXL检索现有的条件格式规则吗?

是的,您可以使用IronXL提供的GetConditionalFormattingAt方法检索现有的条件格式规则。这使您能够访问和管理应用于Excel电子表格中的单元格的规则。

在IronXL中可以使用哪些运算符进行条件格式?

IronXL支持多种条件格式运算符,包括NoComparison、Between、NotBetween、Equal、NotEqual、GreaterThan、LessThan、GreaterThanOrEqual和LessThanOrEqual。

检索后可以修改条件格式规则吗?

虽然大多数检索到的条件格式规则的属性不能被修改,但您可以使用IronXL中的PatternFormatting属性调整BackgroundColor

如何从单元格范围中删除条件格式规则?

要从单元格范围中删除条件格式规则,请使用IronXL中的RemoveConditionalFormatting方法并提供您希望删除的规则的索引。

在IronXL中,条件格式可用哪些样式选项?

使用IronXL,您可以在条件格式中应用多种风格,如字体和大小调整、边框、对齐设置以及背景模式和颜色。

使用IronXL进行条件格式的第一步是什么?

第一步是从NuGet下载IronXL.Excel库,使您能够在Excel电子表格中使用C#实施条件格式。

Chaknith Bin
软件工程师
Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。
准备开始了吗?
Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布