Introducing a lightweight library that effortlessly converts your tabular data into well-structured .xlsx Excel files. With features like autofiltering, adjustable column widths, and native date formatting, it simplifies data management in both Node and browsers. Compact and efficient, it empowers you to create professional-quality spreadsheets without the hassle.
xlsxtable is a lightweight and user-friendly library designed for effortlessly creating polished .xlsx
Excel files from tabular data. With its intuitive features and minimal footprint (under 7KB gzipped), this library is an ideal solution for generating Excel reports across various environments, including Node.js and web browsers.
Key Features:
- Enhanced Headings: Automatically emboldens and allows the option to freeze and apply autofilters to headings.
- Dynamic Column Widths: Adjusts column widths based on the contents of the cells for optimal readability.
- Date and Time Handling: Converts date and time values into native Excel formats, ensuring accurate representation in your spreadsheets.
- Minimal Dependencies: The library depends solely on littlezipper, which is also lightweight and has no additional runtime dependencies.
This project serves as the backbone for enabling .xlsx
downloads in sophisticated applications, such as the Neon SQL Editor, as highlighted in this blog post.
Pronunciation:
The name xlsxtable is pronounced as ex-el-ess-EX-tuh-bl, which rhymes with Hextable.
Supported Data Types:
Types are defined specifically for each column and include:
- String: Cell values are coerced to strings.
- Number: Accepts both numeric types and numeric strings.
- Dates/Times: Features local and UTC variants for accurate time representation, utilizing JavaScript
Date
objects. Unsupported dates can fallback to string formats. - Empty Cells: Any null or undefined cell values will result in empty cells in the generated Excel file.
Example Usage:
Here's a simple example illustrating how to write an .xlsx
file in Node.js:
import { createXlsx, XlsxTypes as Xl } from 'xlsxtable';
import { writeFileSync } from 'fs';
const now = new Date();
createXlsx({
// sheet data
headings: ['id', 'name', 'dob', 'wake_up', 'lastUpdated'],
types: [Xl.Number, Xl.String, Xl.LocalDate, Xl.LocalTime, Xl.LocalDateTime],
data: [
[1, 'Anna', new Date(1979, 0, 1), new Date(0, 0, 0, 7), now],
[2, 'Bryn', new Date(1989, 1, 2), new Date(0, 0, 0, 8), now],
[3, 'Chip', new Date(1999, 2, 3), new Date(0, 0, 0, 9), now],
],
// options
sheetName: 'Sheet 1', // shown on the tab at the bottom: limited character range allowed
freeze: true, // freeze the top/header row
autoFilter: true, // enable autofilter for headers
wrapText: true, // wrap long text cells
// metadata
creator: 'Diane',
title: 'Blughupsnitch data',
description: 'Data about the blughupsnitch',
company: 'Dogoodnever Inc.',
})
.then(xlsx => writeFileSync('/path/to/my.xlsx', xlsx));
To enable download functionality in web browsers, you can use the following code:
const xlsx = await createXlsx(/* ... */);
const url = URL.createObjectURL(new Blob([xlsx]));
const link = document.createElement('a');
link.style.display = 'none';
document.body.appendChild(link);
link.href = url;
link.download = 'my.xlsx';
link.click();
setTimeout(() => {
URL.revokeObjectURL(url);
document.body.removeChild(link);
}, 0);
With xlsxtable, generating professional Excel files from your data is just a few lines of code away!