Excel for .NET
条件付き書式

Excel supports conditional formatting, where you can highlight cells in different styles based on specific variations in the data. Conditional formatting enables you to analyze the data, detect critical conditions and identify certain data types. The appearance of cells is changed on the basis of whether the specified conditions are true or false.

The following image showcases conditional formatting applied in Excel worksheet, where cell values ranging between 1 to 18 are highlighted.

In Excel, you can implement conditional formatting using the XLConditionalFormatting class. The XLConditionalFormatting class provides Rules and Ranges properties for defining rules and ranges, respectively to apply conditional formatting.

To apply conditional formatting in Excel, use the following code. In this example, first we add data to the sheet which displays name and age of five persons and then use conditional formatting to highlight the cells that show the values between 1 and 18.

C#
コードのコピー
var conditionalFormat = new XLConditionalFormatting();
conditionalFormat.Ranges.Add(new XLRange(1, 1, 5, 1));
var rule = new XLConditionalFormattingRule();
rule.Type = XLConditionalFormattingType.CellIs;
rule.Operator = XLConditionalFormattingOperator.Between;
rule.FirstFormula = "1";
rule.SecondFormula = "18";
rule.Pattern = new XLPatternFormatting() { BackColor = Color.Red };
conditionalFormat.Rules.Add(rule);
sheet4.ConditionalFormattings.Add(conditionalFormat);