---
title: "Add Excel data bars in C#"
url: https://recursiveai.org/onedocument/cookbook/add-excel-conditional-formatting-data-bars/
description: "Add Excel conditional formatting data bars in C# with OneDocument. Save a rule over numeric cells, then reopen the XLSX to read its range and color."
version: "1.0.0"
---

# Add Excel data bars in C#

**OneDocument** is a free, open-source Word, Excel and PowerPoint document library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneDocument.Spreadsheets) · [GitHub](https://github.com/recursive-ai-oss/onedocument-csharp) · [Docs](https://recursiveai.org/onedocument/docs/) · [Download example (.zip)](https://recursiveai.org/onedocument/cookbook/add-excel-conditional-formatting-data-bars/add-excel-conditional-formatting-data-bars.zip)

Data bars compare numbers within a range.

```shell
dotnet add package \
    RecursiveAI.OneDocument.Spreadsheets
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;
using var book = Workbook.Open("units.xlsx");
var range = CellRange.Parse("A2:A4");
var blue = StyleColor.FromRgb(0x638EC6);
book.Sheets[0].ConditionalFormats.Add(range,
    ConditionalRuleSpec.DataBars(blue));
book.SaveAs("bars.xlsx");
Console.WriteLine("Wrote bars.xlsx");
```
Unzip [the example](https://recursiveai.org/onedocument/cookbook/add-excel-conditional-formatting-data-bars/add-excel-conditional-formatting-data-bars.zip), then `dotnet run`.

```text
Wrote bars.xlsx
```

`units.xlsx` is in the zip: A1 is “Units sold” and A2:A4 contain 12, 30, and 55. Open `bars.xlsx` in Excel to see the bars; the program saves the conditional formatting rule, and Excel draws the bars.

## The full program

The full program reopens the saved XLSX and reads the stored rule.

```csharp
using RecursiveAI.OneDocument.Spreadsheets;

using var book = Workbook.Open("units.xlsx");
var sheet = book.Sheets[0];

var range = CellRange.Parse("A2:A4");
var blue = StyleColor.FromRgb(0x638EC6);
var rule = ConditionalRuleSpec.DataBars(blue);
sheet.ConditionalFormats.Add(range, rule);
book.SaveAs("bars.xlsx");

using var saved = Workbook.Open("bars.xlsx");
var format = saved.Sheets[0].ConditionalFormats.Single();
var bar = format.Rules.Single().DataBar
    ?? throw new InvalidOperationException("No data bar rule found.");
Console.WriteLine($"Range: {format.Ranges.Single()}");
Console.WriteLine($"Color (ARGB): #{bar.Color.Value:X8}");
```

```text
Range: A2:A4
Color (ARGB): #FF638EC6
```

If you see no bars in an existing worksheet, check the rule’s range and make sure the cells contain numbers, not numeric text. To write a number, call `SetNumber()` on the cell. In the full program, `sheet` is the first worksheet:

```csharp
sheet["A2"].SetNumber(12);
```

## Variations

**Show only the bars.** Replace the `rule` assignment in the full program with this. The underlying numbers stay in the cells.

```csharp
var rule = new ConditionalRuleSpec
{
    Type = ConditionalRuleType.DataBar,
    DataBar = new DataBarInfo(
        [new("min"), new("max")], blue, ShowValue: false)
};
```

**Change the color.** Replace the `blue` assignment with `var blue = StyleColor.FromRgb(0x63BE7B);` for green bars.

## Related recipes

- [Create an Excel XLSX file in C#](https://recursiveai.org/onedocument/cookbook/create-excel-file/)
- [Read an Excel file in C#](https://recursiveai.org/onedocument/cookbook/read-excel-file/)

---

## The short answer

Create a `ConditionalRuleSpec.DataBars()` rule with the color you want and add it to `Worksheet.ConditionalFormats` with the numeric range. Save the workbook as XLSX. In Excel, bars appear inside the original cells: no chart object and no extra data column are needed.

## How do I insert data bars in conditional formatting in Excel?

Apply a `DataBars()` rule to the numeric range, leaving out the header row.

In the sample, parse `A2:A4` with `CellRange.Parse()`, create the rule with the bar color, and add it to `sheet.ConditionalFormats` before saving. The `DataBars()` factory sets the shortest and longest endpoints to the range’s minimum and maximum. With the sample data, 12 gets the shortest bar and 55 gets the longest. The underlying values are still available to formulas.

Choose the range to match the comparison you want. A rule over one month’s sales compares rows in that month. A rule over two months’ sales together compares across both. Leave totals outside the range: an extreme value can stretch the scale and make the middle values’ bars look similar.

## How do I create solid green data bars in Excel?

The green variation sets the RGB color. The reopened rule reports ARGB, which puts an alpha byte before the red, green, and blue bytes: `63BE7B` becomes `FF63BE7B`. Color and fill style are separate settings. OneDocument 1.0.0 writes standard data bar rules; it does not author the extended rules used for choices such as solid fill. If you need that appearance, set it in Excel after opening the workbook.
