html-to-xlsx-styled
v1.0.0
Published
Export HTML <table> elements to styled .xlsx workbooks (merges, CSS styles) using xlsx-js-style.
Maintainers
Readme
html-to-xlsx-styled
Convert an HTML <table> into a styled .xlsx workbook. Cell merges (rowspan / colspan), text content, and CSS-derived cell styles are mapped to Excel using xlsx-js-style.
Install
npm install html-to-xlsx-styledPeer expectation: use this in environments where you can obtain a real HTMLTableElement (browser DOM, or a DOM implementation such as jsdom in Node).
Quick start (browser)
<table id="data">
<tr><th>Name</th><th>Score</th></tr>
<tr><td>Ada</td><td>100</td></tr>
</table>import { exportHtmlTableToExcel } from 'html-to-xlsx-styled';
const table = document.getElementById('data');
// Get a Blob (default in browsers) and optionally trigger download
const blob = exportHtmlTableToExcel(table, {
sheetName: 'Results',
download: true,
fileName: 'results.xlsx',
});API
exportHtmlTableToExcel(table, options?)
Serializes the table to .xlsx bytes.
- Returns — By default: a
BlobwhenBlobexists (typical browser), otherwiseUint8Array. Override withoutputType. - Options (
ExportOptions):sheetName— Excel sheet name (invalid characters are sanitized; max length enforced).fileName— Used whendownloadistrue(default file name:export.xlsx).download— Iftrue, starts a browser download via a temporary object URL. RequiresdocumentandURL.createObjectURL; otherwise throwsExcelTableExportErrorwith codeDOM_REQUIRED. The function still returns the same binary result as whendownloadisfalse.useComputedStyles— Whentrue(default), styles are taken from computed CSS. Set tofalseto rely on inline styles only (faster, fewer dependencies on full layout).includeEmptyCellsStyles— Whentrue, cells with no text but with visible styling may be written so formatting is preserved. Defaultfalse.outputType—'blob' | 'arrayBuffer' | 'uint8Array'. If you need bytes in Node or withoutBlob, use'uint8Array'or'arrayBuffer'.
createWorkbookFromTable(table, options?)
Builds an xlsx-js-style WorkBook from the table. Use this when you want to merge sheets, post-process cells, or write the file yourself instead of using exportHtmlTableToExcel.
import * as XLSX from 'xlsx-js-style';
import { createWorkbookFromTable } from 'html-to-xlsx-styled';
const wb = createWorkbookFromTable(table, { sheetName: 'Sheet1' });
const buf = XLSX.write(wb, { bookType: 'xlsx', type: 'array', cellStyles: true });ExcelTableExportError
Thrown for invalid input or unsupported environment behavior. Inspect error.code:
| Code | Meaning |
|-------------------|---------|
| INVALID_TABLE | Value is not an HTML table element. |
| DOM_REQUIRED | Operation needs a browser DOM (e.g. download: true without document / URL). |
| NOT_IMPLEMENTED | Reserved. |
| UNKNOWN | Other errors (e.g. unsupported outputType). |
Node / tests
Provide a HTMLTableElement from your DOM library of choice (for example jsdom):
import { JSDOM } from 'jsdom';
import { exportHtmlTableToExcel } from 'html-to-xlsx-styled';
const { window } = new JSDOM(`<table><tr><td>hi</td></tr></table>`);
const table = window.document.querySelector('table');
const bytes = exportHtmlTableToExcel(table, {
outputType: 'uint8Array',
useComputedStyles: false,
});License
ISC
