Working with XLSX in TypeScript
Excel (.xlsx) file handling with sheet selection, advanced header processing, and high-performance data operations.
Installation
Section titled “Installation”npm install fairspecGetting Started
Section titled “Getting Started”The XLSX plugin provides:
loadXlsxTable- Load Excel files into tablessaveXlsxTable- Save tables to Excel filesXlsxPlugin- Plugin for framework integration
For example:
import { loadXlsxTable } from "fairspec"
const table = await loadXlsxTable({ data: "table.xlsx" })// the column types will be automatically inferredBasic Usage
Section titled “Basic Usage”Loading XLSX Files
Section titled “Loading XLSX Files”import { loadXlsxTable } from "fairspec"
// Load a simple XLSX fileconst table = await loadXlsxTable({ data: "data.xlsx" })
// Load with custom format (specify sheet)const table = await loadXlsxTable({ data: "data.xlsx", format: { name: "xlsx", sheetName: "Sheet2" }})
// Load multiple XLSX files (concatenated)const table = await loadXlsxTable({ data: ["part1.xlsx", "part2.xlsx", "part3.xlsx"]})Saving XLSX Files
Section titled “Saving XLSX Files”import { saveXlsxTable } from "fairspec"
// Save with default optionsawait saveXlsxTable(table, { path: "output.xlsx" })
// Save with custom sheet nameawait saveXlsxTable(table, { path: "output.xlsx", format: { name: "xlsx", sheetName: "Data" }})Advanced Features
Section titled “Advanced Features”Sheet Selection
Section titled “Sheet Selection”// Select by sheet number (1-indexed)const table = await loadXlsxTable({ data: "workbook.xlsx", format: { name: "xlsx", sheetNumber: 2 // Load second sheet }})
// Select by sheet nameconst table = await loadXlsxTable({ data: "workbook.xlsx", format: { name: "xlsx", sheetName: "Sales Data" }})Multi-Header Row Processing
Section titled “Multi-Header Row Processing”// XLSX with multiple header rowsconst table = await loadXlsxTable({ data: "multi-header.xlsx", format: { name: "xlsx", headerRows: [1, 2], headerJoin: "_" }})// Resulting columns: ["Year_Quarter", "2023_Q1", "2023_Q2", "2024_Q1", "2024_Q2"]Comment Row Handling
Section titled “Comment Row Handling”// Skip specific comment rowsconst table = await loadXlsxTable({ data: "with-comments.xlsx", format: { name: "xlsx", commentRows: [1, 2], headerRows: [3] }})
// Skip rows with comment prefixconst table = await loadXlsxTable({ data: "data.xlsx", format: { name: "xlsx", commentPrefix: "#", headerRows: [1] }})Remote File Loading
Section titled “Remote File Loading”// Load from URLconst table = await loadXlsxTable({ data: "https://example.com/data.xlsx"})
// Load multiple remote filesconst table = await loadXlsxTable({ data: [ "https://api.example.com/data-2023.xlsx", "https://api.example.com/data-2024.xlsx" ]})Column Selection
Section titled “Column Selection”// Select specific columnsconst table = await loadXlsxTable({ data: "data.xlsx", format: { name: "xlsx", columnNames: ["name", "age", "city"] }})