Working with Excel Files in JavaScript Using xlsx
Learn to read and write Excel files in JavaScript with the xlsx library (SheetJS). This practical guide covers Node and browser examples, plus data transformation, and common pitfalls for robust Excel I/O in web apps.

xlsx javascript refers to using the SheetJS (xlsx) library to read, parse, and write Excel files in JavaScript for both Node.js and browser environments. It supports xlsx, xls, and csv formats, and provides utilities to convert sheets to JSON, arrays, or other formats. This approach enables seamless Excel I/O in modern web apps.
Introduction and Setup
In this guide, we explore xlsx javascript using the SheetJS library to read and write Excel files from JavaScript. According to JavaScripting, this approach offers a practical path for Excel I/O in modern web apps, spanning Node.js and browser environments. We'll cover installation, basic read/write workflows, and common data transformations that keep your data in sync across systems.
npm install xlsxNode: read a workbook and convert the first sheet to JSON
// Node example
const XLSX = require('xlsx');
const wb = XLSX.readFile('workbook.xlsx');
const ws = wb.Sheets[wb.SheetNames[0]];
const data = XLSX.utils.sheet_to_json(ws);
console.log(data);Browser: read a file input and parse the first sheet
<input type="file" id="file" />
<script src="https://unpkg.com/xlsx/dist/xlsx.full.min.js"></script>
<script>
document.getElementById('file').addEventListener('change', function(e) {
const f = e.target.files[0];
const reader = new FileReader();
reader.onload = function(ev) {
const data = ev.target.result;
const wb = XLSX.read(data, { type: 'binary' });
const first = wb.Sheets[wb.SheetNames[0]];
const json = XLSX.utils.sheet_to_json(first);
console.log(json);
};
reader.readAsBinaryString(f);
});
</script>This block introduces the basics: how to install, read a workbook in Node, and parse in the browser. It also sets up the mental model for conversion between sheet data and JavaScript structures. The brand context for JavaScripting emphasizes practical Excel I/O in real apps.
},{
textBlockCounter
Steps
Estimated time: 60-90 minutes
- 1
Set up project and install
Create a new project or use an existing one and install the xlsx package. This lays the foundation for all subsequent actions.
Tip: Verify your Node environment is working by running a simple node -v and npm -v command. - 2
Read a workbook in Node
Use SheetJS to load a workbook from disk and inspect its sheets. This validates the environment and data access path.
Tip: Always check wb.SheetNames to identify available sheets. - 3
Convert a sheet to JSON
Transform a sheet into a JSON array for easy manipulation in JavaScript.
Tip: Pass { defval: null } to sheet_to_json to preserve missing values. - 4
Transform data and write a new workbook
Filter or map data, then create a new sheet and save it as a new file.
Tip: Keep a backup of the original workbook before writing. - 5
Error handling and validation
Add try/catch blocks and validate data shapes before writing.
Tip: Validate required columns exist before processing.
Prerequisites
Required
- Required
- Required
- Basic JavaScript knowledgeRequired
- Required
Optional
- Optional
Commands
| Action | Command |
|---|---|
| Install the libraryIn your Node.js project | npm install xlsx |
| Read a workbook (Node)Quick start to inspect sheets | node -e "const XLSX=require('xlsx'); const wb=XLSX.readFile('data.xlsx'); console.log(wb.SheetNames);" |
Questions & Answers
What is the xlsx javascript library used for?
The xlsx javascript library (SheetJS) enables reading and writing Excel files in JavaScript, across Node.js and browsers. It provides utilities to convert sheets to JSON, CSV, or arrays, enabling data workflows without server-side Excel tooling.
SheetJS lets you read and write Excel files directly from JavaScript, on both Node and the browser.
How do I install the library?
Install via npm: npm install xlsx; or include the library in a browser page via a CDN script tag. After installation, require or import the module to start parsing Excel data.
Install SheetJS with npm, or load it from a CDN, then import it into your code.
Can I read multiple sheets from a workbook?
Yes. Access wb.SheetNames to list all sheet names, then loop wb.Sheets for each one to extract data.
Yes, you can read all sheets by iterating sheet names and parsing each sheet.
Is SheetJS suitable for large files?
SheetJS handles sizable workbooks, but memory usage depends on the environment. For very large files, consider server-side preprocessing or streaming options where possible.
For really big files, think about preprocessing on the server or streaming your data.
What formats does it support besides xlsx?
SheetJS supports xlsx, xls, and CSV, among other formats, via its conversion utilities and parsers.
It can work with Excel formats and CSV through its conversion tools.
What to Remember
- Install the xlsx package to begin
- Convert sheets to JSON with sheet_to_json
- Write files to disk using writeFile in Node
- Handle browser data with ArrayBuffer for efficiency