如何對單元格應用條件格式設置
條件格式是試算表和資料處理軟體中的一項功能,它允許您根據特定條件或標準,對儲存格或數據應用特定的格式化樣式或規則。 它使您能够在视觉上突出或强调满足特定条件的数据,使得在电子表格或表中分析和理解数据变得更加容易。
輕鬆使用IronXL添加、檢索和移除條件格式。 當您使用樣式添加條件格式時,您可以使字體和尺寸調整,設置邊框和對齊,並定義背景圖案和顏色
如何對單元格應用條件格式設置
開始使用IronXL
立即在您的專案中使用IronXL,並享受免費試用。
加入條件格式範例
條件格式化包含了當單元格符合指定規則標準時應用的規則和樣式。 The styles can include字體和尺寸調整, 邊框和對齊設置以及背景圖案和顏色.
要定義規則,請使用 ConditionalFormatting 提供的 CreateConditionalFormattingRule
方法。 將此方法返回的對象指派給一個變數,並使用這個變數來應用所需的樣式。 最後,使用 AddConditionalFormatting
方法,並提供創建的規則及其應用的儲存格範圍。
: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")
之前
後
以下是所有可用的規則:
- NoComparison:預設值。
- Between:'Between' 運算子
- NotBetween:'不介於'運算子
- 等於:「等於」運算符
- NotEqual:「不等於」運算符
- 大於:'大於'運算符
- LessThan:「小於」運算子
- GreaterThanOrEqual:「大於或等於」運算子
- LessThanOrEqual:「小於或等於」運算符
檢索條件格式範例
要檢索條件格式規則,請使用 GetConditionalFormattingAt
方法。 此方法返回的規則可能包含多個規則,您可以使用 GetRule
方法來選擇特定的一個。 雖然無法修改擷取規則的大多數屬性,但您可以通過訪問 PatternFormatting 屬性來調整 BackgroundColor。 以下程式碼展示了這個概念。
: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")
之前
後
移除條件格式範例
要移除條件格式規則,請使用 RemoveConditionalFormatting
方法。 將目標條件格式規則的索引傳遞給方法。
: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")