如何對單元格應用條件格式設置
條件格式是一種試算表和數據處理軟件中的功能,可以根據某些條件或標準對單元格或數據應用特定的格式樣式或規則。它允許您視覺上突出或強調符合特定條件的數據,使您在試算表或表格中更容易分析和理解數據。
使用 IronXL 輕鬆添加、檢索和刪除條件格式。當使用樣式添加條件格式時,您可以使 字體和尺寸調整,設置 邊框和對齊,並定義 背景圖案和顏色
如何對單元格應用條件格式設置
立即開始在您的專案中使用IronPDF,並享受免費試用。
查看 IronXL 上 Nuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變Excel。
Install-Package IronXL.Excel
請考慮安裝 IronXL DLL 直接下載並手動安裝到您的專案或GAC表單: IronXL.zip
手動安裝到您的項目中
下載DLL添加條件格式範例
條件格式包括在儲存格符合指定規則條件時應用的規則和樣式。這些樣式可以包括 字體和尺寸調整, 邊框和對齊設置以及 背景圖案和顏色要定義規則,請使用由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: '介於' 操作符
- NotBetween: '不介於' 操作符
- Equal: '等於' 操作符
- NotEqual: '不等於' 操作符
- GreaterThan: '大於' 操作符
- 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")