simple-xlsx-export
v1.0.0
Published
Zero-dependency library for exporting data to XLSX files
Downloads
20
Maintainers
Readme
simple-xlsx-export
A zero-dependency JavaScript/TypeScript library for exporting data to XLSX (Excel) files. No external libraries required - generates valid Excel files easily - for example as export from grids in applications, where no heavy libraries are needed.
Features
Zero Dependencies - No external libraries required
Small Bundle Size - Lightweight implementation
Simple API - Just two functions to export data
Security - Protection against formula injection and common vulnerabilities
Installation
npm install simple-xlsx-exportQuick Start
Recommended: Use exportFull() for Most Cases
For any data with numbers, dates, or when you want column widths, use exportFull(). It auto-detects types and is the easiest option!
import { exportFull, saveToFile } from 'simple-xlsx-export';
const data = [
[
{ data: 'Name', width: 20 },
{ data: 'Age', width: 10 },
{ data: 'Salary', width: 15 }
],
[
{ data: 'John Doe' },
{ data: 30 }, // Auto-detected as number!
{ data: 75000 } // Works in Excel formulas!
],
[
{ data: 'Jane Smith' },
{ data: 25 },
{ data: 65000 }
]
];
const xlsxData = exportFull(data, 'employees');
await saveToFile(xlsxData, 'employees.xlsx');Alternative: exportOnlyString() for Text-Only Data
Only use this if you have pure text data with no numbers or dates:
import { exportOnlyString, saveToFile } from 'simple-xlsx-export';
const data = [
['Name', 'City', 'Country'],
['John Doe', 'New York', 'USA'],
['Jane Smith', 'London', 'UK']
];
const xlsxData = exportOnlyString(data, 'addresses');
await saveToFile(xlsxData, 'addresses.xlsx');
// Note: All values become text in Excel, even if they look like numbersKey Differences
| Feature | exportOnlyString | exportFull |
|---------|------------------|------------|
| Input | string[][] | CellData[][] |
| Type detection | ❌ All text | ✅ Automatic! |
| Numbers work in formulas | ❌ No | ✅ Yes |
| Column widths | ❌ No | ✅ Yes |
| Best for | Pure text | Everything else |
All Supported Data Types Example
import { exportFull, saveToFile } from 'simple-xlsx-export';
const data = [
[
{ data: 'Name', width: 20 },
{ data: 'Age', width: 10 },
{ data: 'Salary', width: 15 },
{ data: 'Start Date', width: 15 },
{ data: 'Active', width: 10 }
],
[
{ data: 'John Doe' }, // string (auto)
{ data: 30 }, // number (auto)
{ data: 75000 }, // number (auto)
{ data: new Date('2020-01-15') }, // date (auto)
{ data: true } // boolean (auto)
]
];
const xlsxData = exportFull(data, 'employees');
await saveToFile(xlsxData, 'employees.xlsx');
// Explicit types (optional - auto-detection usually works)
const explicitTypes = [[
{ data: '100', type: 'string' }, // Force as text
{ data: '200', type: 'number' } // Force as number
]];API Reference
exportOnlyString(data, filename, sheetName?)
Exports a simple 2D array of strings to XLSX format. All values become text in Excel.
Parameters:
data: string[][]- Two-dimensional array of stringsfilename: string- Output filename (without .xlsx extension)sheetName?: string- Optional sheet name (default: 'Sheet1')
Returns: Uint8Array - The XLSX file data
exportFull(data, filename, sheetName?)
Exports a 2D array of CellData objects with auto-type detection and formatting (recommended).
Parameters:
data: CellData[][]- Two-dimensional array of CellData objectsfilename: string- Output filename (without .xlsx extension)sheetName?: string- Optional sheet name (default: 'Sheet1')
Returns: Uint8Array - The XLSX file data
saveToFile(xlsxData, filename)
Saves XLSX data to a file (Node.js only).
Parameters:
xlsxData: Uint8Array- The XLSX file data from export functionsfilename: string- Output filename (will add .xlsx if missing)
Returns: Promise<void>
downloadInBrowser(xlsxData, filename)
Triggers a download in the browser.
Parameters:
xlsxData: Uint8Array- The XLSX file data from export functionsfilename: string- Download filename (will add .xlsx if missing)
CellData Interface
interface CellData {
data: string | number | boolean | Date;
type?: 'string' | 'number' | 'boolean' | 'date'; // Auto-detected if omitted
width?: number; // Column width in character units (0-255)
}Supported Data Types
- string - Text data
- number - Numeric values
- boolean - TRUE/FALSE values
- date - Date objects (formatted as dates in Excel)
Browser Usage
Requires a bundler (Vite, Webpack, Parcel, etc.)
import { exportFull, downloadInBrowser } from 'simple-xlsx-export';
// In your click handler
const xlsx = exportFull(data, 'report');
downloadInBrowser(xlsx, 'report.xlsx');See QUICKSTART.md for React and other framework examples.
Security
Built-in protection against common vulnerabilities:
- Formula Injection Prevention - Automatically sanitizes dangerous formulas
- Input Validation - Enforces Excel's limits (1,048,576 rows, 16,384 columns)
- XML Escaping - Properly escapes all user data
- No Dependencies - Zero supply chain attack surface
Why Zero Dependencies?
- Smaller bundle - No bloat (~17 kB)
- Better security - No supply chain vulnerabilities
- Easier maintenance - No dependency updates needed
- Full control - Complete understanding of the codebase
Compatibility
Node.js: 14+
Browsers: All modern browsers (Chrome, Firefox, Safari, Edge)
License
MIT License - see LICENSE file for details.
Contributing
Contributions welcome! Read CONTRIBUTING.md first.
Core principles: Zero dependencies, extreme simplicity, small package size.
Repository
Simple, focused XLSX export. That's it.
