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剛剛發布