Skip to content

Styling

Sveinung edited this page Sep 14, 2025 · 1 revision

Cell styling

Styles are represented with the Style class. To use a style on a cell, we need to first get the corresponding StyleId. We get that by calling Spreadsheet.AddStyle(Style) which returns a StyleId. To use the style on a cell, we then use one of the constructor overloads for the cell that takes a StyleId.

Example:

var style = new Style();
style.Fill.Color = Color.Yellow;
style.Font.Bold = true;

await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream);
await spreadsheet.StartWorksheetAsync("Sheet 1");

var styleId = spreadsheet.AddStyle(style);

var cell = new Cell("My styled cell", styleId);
await spreadsheet.AddRowAsync([cell]);

await spreadsheet.FinishAsync();

Default styling

If the style should be applied for all cells in a column, then we can use the DefaultStyle property on the column options. Here is an example:

var style = new Style();
style.Fill.Color = Color.Yellow;
style.Font.Bold = true;

await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream);

var worksheetOptions = new WorksheetOptions();
worksheetOptions.Column(1).DefaultStyle = style;

await spreadsheet.StartWorksheetAsync("Sheet 1", worksheetOptions);

var cell = new Cell("My styled cell");
await spreadsheet.AddRowAsync([cell]);

await spreadsheet.FinishAsync();

Similarly, if the style should be applied for all cells in a row, then we can use the DefaultStyle property on the RowOptions class. Here is an example:

var style = new Style();
style.Fill.Color = Color.Yellow;
style.Font.Bold = true;

await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream);
await spreadsheet.StartWorksheetAsync("Sheet 1");

var rowOptions = new RowOptions { DefaultStyle = style };

var cell = new Cell("My styled cell");
await spreadsheet.AddRowAsync([cell], rowOptions);

await spreadsheet.FinishAsync();

In cases where there is a combination of cell, row, and column styles, the most specific style will be applied to the cell. Only the most specific style will be applied, no parts of a less specific style will be inherited. This is the order from least to most specific styling:

  1. Column style
  2. Row style
  3. Cell style

Default font

By default, the font for all cells will be Calibri with size 11. This can be changed by setting SpreadCheetahOptions.DefaultFont. If set, the font will be applied to all cells, including those with some form of default styling, unless a more specific font name and size is specified.

Example:

var defaultFont = new DefaultFont
{
    Name = "Consolas",
    Size = 14
};

var options = new SpreadCheetahOptions { DefaultFont = defaultFont };

await using var spreadsheet = await Spreadsheet.CreateNewAsync(stream, options);
await spreadsheet.StartWorksheetAsync("Sheet 1");

var cell = new Cell("My cell value");
await spreadsheet.AddRowAsync([cell]);

await spreadsheet.FinishAsync();

Clone this wiki locally