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…
Exporting multi-sheet .xlsx spreadsheets with formatted currency headers and live formulas from a lightweight Node.js script is possible without pulling in a multi-megabyte dependency chain. According to the source, an .xlsx file is essentially a standard ZIP archive containing small UTF-8 XML files that adhere to ECMA-376 OpenXML.
With knowledge of the three strict invariants Microsoft Excel and LibreOffice Calc expects, it's possible to emit rock-solid workbooks in just 150 lines of Node.js with zero npm spreadsheet dependencies.
To achieve this, the minimum archive layout requires three parts: Content_Types.xml, _rels/.rels, and xl/, which includes workbook.xml, _rels/workbook.xml.rels, styles.xml, and worksheets containing sheet1.xml and sheet2.xml. The cellXml function is used to serialize cell data into XML format, with special attention paid to avoiding leading "=" in f tags and caching precomputed values alongside f tags to ensure non-evaluating previewers display real numbers immediately.
Additionally, styles.xml must reserve fills 0 and 1 for Excel to correctly render background colors and custom number formats require using numFmtId = 164 for currency formats.
Lastly, row ordering and column letters must adhere to strict ascending order, with rows and cells appearing in the correct order before stringifying to prevent XML validation failures. By following these guidelines, it's possible to create reliable .xlsx files without relying on heavy npm dependencies.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to