Excel 条件格式
using IronXL;
using IronXL.Formatting.Enums;
using IronXL.Styles;

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.FontFormatting.IsBold = true;
rule.FontFormatting.FontColor = "#123456";
rule.BorderFormatting.RightBorderColor = "#ffffff";
rule.BorderFormatting.RightBorderType = BorderType.Thick;
rule.PatternFormatting.BackgroundColor = "#54bdd9";
rule.PatternFormatting.FillPattern = FillPattern.Diamonds;
// Apply formatting on specified region
workSheet.ConditionalFormatting.AddConditionalFormatting("A3:A8", rule);

// Create conditional formatting rule
var rule1 = workSheet.ConditionalFormatting.CreateConditionalFormattingRule(ComparisonOperator.Between, "7", "10");
// Set style options
rule1.FontFormatting.IsItalic = true;
rule1.FontFormatting.UnderlineType = FontUnderlineType.Single;
// Apply formatting on specified region
workSheet.ConditionalFormatting.AddConditionalFormatting("A3:A9", rule1);

workBook.SaveAs("applyConditionalFormatting.xlsx");
NuGet Download
PM > Install-Package IronXL.Excel
Excel 条件格式

Excel 条件格式

IronXL 库支持单元格和区域的条件格式设置。 条件格式允许根据逻辑/程序规则更改单元格的样式,例如背景颜色或文本样式。

使用CreateConditionalFormattingRule(string formula)创建基于布尔公式的条件格式规则。 当公式结果为真时,单元格将被高亮显示。 请注意,要计算的公式必须是布尔函数。

具有三个参数的ComparisonOperator.NotBetween作为第一个参数。

使用条件格式,根据单元格的真/假值(由设定的规则确定),以指定的颜色和格式组合突出显示单元格和区域。 这使得我们能够分析数据、检测问题并识别模式和趋势。

学习如何在 C# 中应用条件格式
在 GitHub 上查看

准备开始了吗?

Nuget Downloads 2,362,812版本:2026.11刚刚发布