Skip to content

Conditional formatting

Sveinung edited this page Nov 13, 2026 · 3 revisions

Conditional formatting can be used to automatically apply a style based on the cell's value or a criteria. A conditional format rule must be added to a cell range to take effect. The cell range must be in A1 reference style, for example A1:C3.

Basic example

await using var outputStream = File.Create("conditional-formatting.xlsx");
await using var spreadsheet = await Spreadsheet.CreateNewAsync(outputStream);

await spreadsheet.StartWorksheetAsync("Sheet");

var style = new ConditionalFormatStyle { Fill = { Color = Color.Yellow } };
var rule = ConditionalFormatRule.UniqueValues().WithStyle(style);

const string cellRange = "A1:E1";
spreadsheet.AddConditionalFormatRule(cellRange, rule);

DataCell[] row = [new(1), new(2), new(3), new(3), new(1)];
await spreadsheet.AddRowAsync(row);

await spreadsheet.FinishAsync();

Result

image

All conditional format rules are created from static methods on ConditionalFormatRule. Here are all the currently supported rules:

  • DuplicateValues()
  • UniqueValues()
  • MatchesFormula(Formula)
  • Values().Between(double, double)
  • Values().NotBetween(double, double)
  • Values().GreaterThan(double)
  • Values().LessThan(double)
  • Values().EqualTo(double)
  • Values().NotEqualTo(double)
  • Values().GreaterThanOrEqualTo(double)
  • Values().LessThanOrEqualTo(double)
  • Values().AboveAverage()
  • Values().BelowAverage()
  • Values().EqualToOrAboveAverage()
  • Values().EqualToOrBelowAverage()
  • Values().StandardDeviationsAboveAverage(int)
  • Values().StandardDeviationsBelowAverage(int)

More types of rules can be added in the future. If you are missing a specific one, please create a new issue about it.

Clone this wiki locally