tabular-exporter — Export Array of Records as Tables
Turn any "array of objects" into styled Excel or BOM-prefixed CSV. Zero dependencies: xlsx is generated in pure JS (ZIP STORE method, built-in CRC32), CSV comes with UTF-8 BOM. No
npm installrequired.
Use Cases
- Export structured data from collections or API responses as deliverable Excel / CSV
- Backend "export" functionality, data reports, data migration
- Need Chinese characters without garbled output, styled headers, frozen first row
Provided Functions
| Function | Description |
|---|---|
exportToExcel(rows, columns, opts?) | Generate styled xlsx (Buffer), optionally write to filePath |
exportToCsv(rows, columns, opts?) | Generate BOM-prefixed CSV string |
exportTable(rows, columns, opts?) | Unified entry point, opts.format = 'xlsx' | 'csv' |
columnsFromKeys(keys, widths?) | Quickly generate column config from a list of keys |
columns has the form [{ key, header?, width? }], rows is an array of objects.
Style features:
- Header: bold + white text + blue background (
4472C4) - Frozen first row
- Zebra striping (even rows light gray background)
- Booleans →
Yes/No; arrays → line breaks; objects → JSON - xlsx compatible with Excel / WPS / LibreOffice (standard OOXML)
Usage
import { exportTable, columnsFromKeys } from './scripts/exporter.mjs';
const rows = [
{ title: 'Wireless Mouse', price: 99.5, inStock: true },
{ title: 'Mechanical Keyboard', price: 299, inStock: false },
];
const columns = [
{ key: 'title', header: 'Title', width: 40 },
{ key: 'price', header: 'Price', width: 12 },
{ key: 'inStock', header: 'In Stock', width: 8 },
];
// Export xlsx (write to file directly)
exportTable(rows, columns, { format: 'xlsx', filePath: 'out.xlsx', sheetName: 'Products' });
// Export csv (returns string)
const csv = exportTable(rows, columns, { format: 'csv' });
Self-test
node scripts/exporter.mjs --selftest