Skip to content

Working with XLSX in TypeScript

Excel (.xlsx) file handling with sheet selection, advanced header processing, and high-performance data operations.

Terminal window
npm install fairspec

The XLSX plugin provides:

  • loadXlsxTable - Load Excel files into tables
  • saveXlsxTable - Save tables to Excel files
  • XlsxPlugin - Plugin for framework integration

For example:

import { loadXlsxTable } from "fairspec"
const table = await loadXlsxTable({ data: "table.xlsx" })
// the column types will be automatically inferred
import { loadXlsxTable } from "fairspec"
// Load a simple XLSX file
const 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"]
})
import { saveXlsxTable } from "fairspec"
// Save with default options
await saveXlsxTable(table, { path: "output.xlsx" })
// Save with custom sheet name
await saveXlsxTable(table, {
path: "output.xlsx",
format: {
name: "xlsx",
sheetName: "Data"
}
})
// Select by sheet number (1-indexed)
const table = await loadXlsxTable({
data: "workbook.xlsx",
format: {
name: "xlsx",
sheetNumber: 2 // Load second sheet
}
})
// Select by sheet name
const table = await loadXlsxTable({
data: "workbook.xlsx",
format: {
name: "xlsx",
sheetName: "Sales Data"
}
})
// XLSX with multiple header rows
const 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"]
// Skip specific comment rows
const table = await loadXlsxTable({
data: "with-comments.xlsx",
format: {
name: "xlsx",
commentRows: [1, 2],
headerRows: [3]
}
})
// Skip rows with comment prefix
const table = await loadXlsxTable({
data: "data.xlsx",
format: {
name: "xlsx",
commentPrefix: "#",
headerRows: [1]
}
})
// Load from URL
const table = await loadXlsxTable({
data: "https://example.com/data.xlsx"
})
// Load multiple remote files
const table = await loadXlsxTable({
data: [
"https://api.example.com/data-2023.xlsx",
"https://api.example.com/data-2024.xlsx"
]
})
// Select specific columns
const table = await loadXlsxTable({
data: "data.xlsx",
format: {
name: "xlsx",
columnNames: ["name", "age", "city"]
}
})