# Styles and number formats

> Style cells, rows and columns with fonts, fills, borders and alignment, and control how numbers and dates display with Excel number formats.

Every cell, row and column can carry an `XlsxCellStyle`. Styles are plain objects; the writer deduplicates them into the workbook's style table, so you can create a fresh style object per cell without bloating the file.

## The style object

**`XlsxCellStyle`** (interface from `@birchui/excel`, full reference: https://docs.birchui.com/api/excel.md)

```ts
interface XlsxCellStyle
```

| Property | Type | Description |
| --- | --- | --- |
| `font?` | `XlsxFont` |  |
| `bold?` | `boolean` |  |
| `italic?` | `boolean` |  |
| `color?` | `string` |  |
| `fill?` | `string` |  |
| `numFmt?` | `string` |  |
| `align?` | `'left' \| 'center' \| 'right'` |  |
| `valign?` | `'top' \| 'center' \| 'bottom'` |  |
| `wrap?` | `boolean` |  |
| `indent?` | `number` |  |
| `border?` | `XlsxBorderSide \| XlsxBorderSides` |  |

The font can be set in two ways: the shortcuts `bold`, `italic` and `color` at the top level, or a full `font` object. When both are present, the shortcut wins.

**`XlsxFont`** (interface from `@birchui/excel`, full reference: https://docs.birchui.com/api/excel.md)

```ts
interface XlsxFont
```

| Property | Type | Description |
| --- | --- | --- |
| `bold?` | `boolean` |  |
| `italic?` | `boolean` |  |
| `underline?` | `boolean` |  |
| `color?` | `string` |  |
| `size?` | `number` |  |
| `name?` | `string` |  |

```ts
const title = { font: { name: 'Arial', size: 14, bold: true, color: '#1F3A5F' } };
const warning = { bold: true, color: '#B42318', fill: '#FEF3F2' };
const note = { italic: true, wrap: true, valign: 'top' as const };
```

| Property | Values | Notes |
| --- | --- | --- |
| `bold`, `italic` | `true` / `false` | Shortcuts for `font.bold` and `font.italic`. |
| `color` | Colour | Text colour; shortcut for `font.color`. |
| `font.underline` | `true` / `false` | Single underline. |
| `font.size` | Points | Default 11. |
| `font.name` | Font family | Default `Calibri`. |
| `fill` | Colour | Solid background. |
| `numFmt` | Format code | See [Number formats](#number-formats). |
| `align` | `'left'`, `'center'`, `'right'` | Horizontal alignment. |
| `valign` | `'top'`, `'center'`, `'bottom'` | Vertical alignment. |
| `wrap` | `true` / `false` | Wrap text within the cell. |
| `indent` | Number of indent levels | Implies left alignment unless `align` is set. Useful for hierarchies. |
| `border` | A side, or `{ top, right, bottom, left }` | See [Borders](#borders). |

### Colours

Colours are hex strings: `'#1F3A5F'`, `'1F3A5F'`, the short form `'#FA0'`, or eight digits with alpha first, `'#FF1F3A5F'`. An invalid colour is ignored and reported in the write warning.

### Borders

A single side object applies to all four sides. An object with `top`, `right`, `bottom` and `left` sets them individually.

**`XlsxBorderSide`** (interface from `@birchui/excel`, full reference: https://docs.birchui.com/api/excel.md)

```ts
interface XlsxBorderSide
```

| Property | Type | Description |
| --- | --- | --- |
| `style` | `'thin' \| 'medium' \| 'thick' \| 'dashed' \| 'dotted' \| 'double'` |  |
| `color?` | `string` |  |

```ts
const boxed = { border: { style: 'thin', color: '#9AA7B8' } };
const totalRow = { bold: true, border: { top: { style: 'thin' }, bottom: { style: 'double' } } };
```

Border styles are `thin`, `medium`, `thick`, `dashed`, `dotted` and `double`. A side without a colour uses Excel's automatic colour.

## Where styles apply

Styles can be set on a column, a row and a cell. They combine property by property, with the more specific one winning:

1. the automatic date format for `Date` values;
2. the column's `style`;
3. the row's `style`;
4. the cell's `style`.

```ts
const sheet: XlsxSheet = {
  name: 'Invoices',
  columns: [{ width: 14 }, { width: 30 }, { width: 14, style: { numFmt: '#,##0.00', align: 'right' } }],
  rows: [
    { cells: ['Invoice', 'Customer', 'Amount'], style: { bold: true, fill: '#F2F4F7' } },
    ['INV-30410', 'Harbour View Dental', 1840.5],
    ['INV-30411', 'Atlas Cold Storage', 12960],
    { cells: ['', 'Total', { value: { formula: 'SUM(C2:C3)', value: 14800.5 }, style: { bold: true } }], style: { fill: '#F9FAFB' } },
  ],
};
```

Here the amount column is right-aligned with two decimals everywhere, the header and total rows get their own fills, and the total cell adds bold on top of the column's format.

Column and row styles are also stored on the column and row themselves, so Excel uses them for empty cells and for anything a user types into that column or row later. To style a single empty cell, write it with a `null` value and a style.

## Number formats

`numFmt` takes an Excel format code, the same text you would type in Excel's **Format Cells › Custom** dialog. The value in the cell stays a number or date; only its display changes.

| Code | 1234.5 shows as | Use for |
| --- | --- | --- |
| `General` | `1234.5` | Default for numbers. |
| `0` | `1235` | Whole numbers. |
| `0.00` | `1234.50` | Fixed decimals. |
| `#,##0` | `1,235` | Thousands separators. |
| `#,##0.00` | `1,234.50` | Money without a symbol. |
| `"$"#,##0.00` | `$1,234.50` | A currency symbol as literal text. |
| `#,##0.00 "EUR"` | `1,234.50 EUR` | A trailing currency code. |
| `#,##0;[Red](#,##0)` | `1,235`, and `(1,235)` in red when negative | Accounting-style negatives. |
| `0%` | `123450%` | Percentages: the value `0.125` shows as `13%`. |
| `0.0%` | `123450.0%` | `0.125` shows as `12.5%`. |
| `0.00E+00` | `1.23E+03` | Scientific notation. |
| `@` | `1234.5` | Treat as text. |

Separators in codes are always written as `,` and `.`. Excel displays them according to the user's regional settings, so `#,##0.00` appears as `1.234,50` for a German user.

### Date and time formats

`Date` values get `yyyy-mm-dd` unless you choose another format:

| Code | Shows as |
| --- | --- |
| `yyyy-mm-dd` | `2026-10-06` |
| `dd/mm/yyyy` | `06/10/2026` |
| `d mmm yyyy` | `6 Oct 2026` |
| `mmmm yyyy` | `October 2026` |
| `ddd d mmm` | `Tue 6 Oct` |
| `hh:mm` | `14:30` |
| `yyyy-mm-dd hh:mm` | `2026-10-06 14:30` |
| `h:mm AM/PM` | `2:30 PM` |
| `[h]:mm` | Durations over 24 hours, such as `37:15` |

A date can also be written as a number with a date format, for example a serial produced by [`dateToSerial()`](https://docs.birchui.com/excel/utilities.md#dates), which is handy when your data already holds spreadsheet serials:

```ts
import { dateToSerial } from '@birchui/excel';

const shipped = dateToSerial(new Date('2026-10-06T09:15:00Z'));
const cell = { value: shipped, style: { numFmt: 'dd mmm yyyy hh:mm' } };
```

Durations are fractions of a day: 1.5 hours is `1.5 / 24`. Write the fraction with a format such as `[h]:mm`.

### Built-in and custom codes

Common codes such as `0`, `0.00`, `#,##0`, `0%`, `mm-dd-yy`, `h:mm` and `@` map to Excel's built-in format ids, which every spreadsheet application knows. Any other code is added to the workbook as a custom format. Both behave the same for the user.

## Styling tips

- **Reuse style objects.** Define styles once as constants. Identical styles are deduplicated either way, but constants make the intent clear.
- **Prefer column styles** for formats that apply to a whole column, such as currency or dates. They keep row data free of style objects.
- **Wrap and set a height** for multi-line text: `wrap: true` on the cell and `height` on the row.
- **Use `indent`** for hierarchies such as cost centres or bills of materials, together with [outline levels](https://docs.birchui.com/excel/layout.md#outlines).
