DEV Community

Cover image for Zero-Dependency OpenXML (.xlsx) in Node.js Without Corrupting Excel Styles
Lark & Lever
Lark & Lever

Posted on

Zero-Dependency OpenXML (.xlsx) in Node.js Without Corrupting Excel Styles

If you have ever needed to export a multi-sheet .xlsx spreadsheet with formatted currency headers and live formulas (SUM, ROUND, IFERROR) from a lightweight Node.js script, pulling in a multi-megabyte dependency chain just to write four worksheets feels excessive.

Under the hood, an .xlsx file is just a standard ZIP archive containing a small handful of UTF-8 XML files conforming to ECMA-376 OpenXML. Once you know the three strict invariants Microsoft Excel and LibreOffice Calc expect, you can emit rock-solid workbooks in ~150 lines of Node.js with zero npm spreadsheet dependencies.

Here are the exact implementation details we used to build the 4-tab OpenXML workbook inside Lark Dev Vault.

1. The Minimum Archive Layout

A valid multi-sheet .xlsx archive only needs these parts:

├── [Content_Types].xml
├── _rels/.rels
└── xl/
    ├── workbook.xml
    ├── _rels/workbook.xml.rels
    ├── styles.xml
    └── worksheets/
        ├── sheet1.xml
        └── sheet2.xml
Enter fullscreen mode Exit fullscreen mode

Using t="inlineStr" on text cells avoids generating a sharedStrings.xml index table altogether, keeping cell serialization stateless and deterministic:

function cellXml(ref, spec) {
  const s = spec.s ?? 1;
  if (spec.f !== undefined) {
    const val = spec.v !== undefined ? `<v>${spec.v}</v>` : "<v>0</v>";
    return `<c r="${ref}" s="${s}"><f>${xmlEsc(spec.f)}</f>${val}</c>`;
  }
  if (typeof spec.v === "number") {
    return `<c r="${ref}" s="${s}"><v>${spec.v}</v></c>`;
  }
  return `<c r="${ref}" t="inlineStr" s="${s}"><is><t>${xmlEsc(spec.v ?? "")}</t></is></c>`;
}
Enter fullscreen mode Exit fullscreen mode

2. Two Gotchas That Silently Corrupt .xlsx Files

Gotcha A: Never put a leading = inside <f>

In Excel's formula bar you type =SUM(D3:D200), but inside OpenXML <f> tags the leading = triggers Excel's "We found a problem with some content..." repair dialog. Always emit the bare expression:

<c r="B7" s="7"><f>SUM(Revenue_Ledger!F3:F200)</f><v>28.65</v></c>
Enter fullscreen mode Exit fullscreen mode

Caching the expected precomputed <v> alongside <f> also guarantees non-evaluating previewers display real numbers immediately, while Excel, LibreOffice Calc, and Google Sheets recalculate dynamically on open.

Gotcha B: styles.xml MUST reserve fills 0 and 1

If you define custom header or highlight cell backgrounds in xl/styles.xml, Excel hard-codes index 0 as "none" and index 1 as "gray125". Omitting those two reserved entries shifts every fillId off-by-two or breaks background rendering entirely:

<fills count="4">
  <fill><patternFill patternType="none"/></fill>
  <fill><patternFill patternType="gray125"/></fill>
  <fill><patternFill patternType="solid"><fgColor rgb="FF1F4D3A"/></patternFill></fill>
  <fill><patternFill patternType="solid"><fgColor rgb="FFEEF6F2"/></patternFill></fill>
</fills>
Enter fullscreen mode Exit fullscreen mode

Custom number formats must also use numFmtId >= 164 ($#,##0.00 for currency, 0.0" hrs" for logged hours) so they don't collide with Excel's built-in format table.

3. Row Ordering & Column Letters

Excel requires <row r="..."> elements and <c r="..."> cells within each row to appear in strictly ascending order. Sorting rows by r and cells by column index before stringifying prevents subtle XML validation failures when programmatic rows are assembled out of order:

function colLetter(idx1) {
  let n = idx1;
  let out = "";
  while (n > 0) {
    const r = (n - 1) % 26;
    out = String.fromCharCode(65 + r) + out;
    n = Math.floor((n - 1) / 26);
  }
  return out;
}
Enter fullscreen mode Exit fullscreen mode

See It in Action

We shipped this exact OpenXML generator inside Lark Dev Vault, alongside an offline Obsidian engineering vault and 12 YAML templates for ADRs, bug root-cause logs, and postmortems:

Top comments (0)