Table of Contents

Conditional formatting

XlsxDocumentSheet.ConditionalFormats lists a sheet's conditional formats and adds, changes, reorders and removes them as Excel's Conditional Formatting does. EffectiveStyle says what a cell looks like with them applied, as Excel draws it, and converting the sheet to PDF or images draws them. Measured against Excel: a rule added here is Excel's own text, and Excel opens what DocWright writes without repair.

dotnet add package DocWright.Spreadsheet

Add rules

using DocWright.Core.Primitives;
using DocWright.Spreadsheet;

XlsxConditionalFormats rules = sales.ConditionalFormats;

// Over 500: Excel's "Light Red Fill with Dark Red Text".
rules.AddCellValue(amountCells, XlsxConditionOperator.GreaterThan, "500", null,
    new XlsxDifferentialStyle { BackgroundColor = ColorRgb.FromRgb(0xFF, 0xC7, 0xCE), FontColor = ColorRgb.FromRgb(0x9C, 0x00, 0x06) });

// The two lowest in bold, values that occur twice in italic.
rules.AddTopBottom(amountCells, 2, percent: false, bottom: true, new XlsxDifferentialStyle { Bold = true });
rules.AddDuplicates(amountCells, unique: false, new XlsxDifferentialStyle { Italic = true });

// A colour scale, data bars, and text that contains "South".
rules.AddColorScale(new XlsxCellRange(2, 2, 9, 2));
rules.AddDataBar(new XlsxCellRange(2, 3, 9, 3));
rules.AddText(new XlsxCellRange(2, 4, 9, 4), XlsxConditionalFormatKind.ContainsText, "south",
    new XlsxDifferentialStyle { BackgroundColor = ColorRgb.FromRgb(0xFF, 0xEB, 0x9C) });

A rule's format is a differential format: it holds only what it changes, and it is added at the end of the workbook's list of them without moving any. A colour scale, a data bar and an icon set draw instead of applying a format. A cell value rule compares text above every number and a blank cell as 0, as Excel does; like a rule Excel adds, it stops the rules after it for a cell it holds for (StopIfTrue).

What a cell looks like

using DocWright.Spreadsheet;

foreach (int row in new[] { 3, 4, 5 })
{
    XlsxEffectiveStyle amount = sales.EffectiveStyle(row, 1);
    XlsxEffectiveStyle bar = sales.EffectiveStyle(row, 3);
    Console.WriteLine($"A{row} {sales.GetCell(row, 1).Value.NumberValue,4}: fill {amount.Style.BackgroundColor?.ToString() ?? "none"}, bold {amount.Style.Bold}, italic {amount.Style.Italic}; bar {bar.DataBarLength:0.00}");
}

The rules are applied as Excel applies them: priority 1 first, a property a higher rule set stays, a rule that stops if true ends the list. A data bar's length is a fraction of the cell's width. A rule given by a formula is not judged until formulas are calculated, and a dates-occurring rule is judged against the day you pass in XlsxConditionalOptions.Today (DocWright never reads the clock); for those, IsComplete is false.

Priority

using DocWright.Spreadsheet;

rules.Reorder(rules[rules.Count - 1], 1);           // the text rule is applied first now
foreach (XlsxConditionalFormat rule in rules.OrderBy(r => r.Priority))
{
    Console.WriteLine($"#{rule.Priority} {rule.Kind}: {rule}");
}

Reorder gives a rule a priority and renumbers the rest from 1. Remove(rule) takes one rule; Remove(range) takes every rule whose cells are all in the range.

sales-conditional.pdf
A sheet of sales amounts: values over 500 in light red, the two lowest in bold, a colour scale from red to green, blue data bars, and regions containing South in yellow.

Download the workbook: sales-conditional.xlsx.

Excel, measured
Colour scale Each colour channel moves from its segment's start toward its end; thresholds by lowest, highest, a number, a percent of the way or a percentile.
Top, bottom, average Numbers only; ties at the threshold pass.
Duplicates Text without regard to case; a blank cell counts as empty text and is never formatted.
Drawn Fills, fonts, borders and colour scales as Excel draws them. A data bar is drawn solid, where Excel's fades to white; icon sets are not drawn yet.

The whole run prints:

A3  480: fill none, bold False, italic False; bar 0.46
A4   75: fill none, bold True, italic True; bar 0.00
A5  950: fill FFC7CE, bold False, italic False; bar 1.00
#1 ContainsText: D2:D9 containsText #1
#2 CellValue: A2:A9 cellIs #2
#3 TopBottom: A2:A9 top10 #3
#4 DuplicateValues: A2:A9 duplicateValues #4
#5 ColorScale: B2:B9 colorScale #5
#6 DataBar: C2:C9 dataBar #6