You read an Excel file in JavaScript by parsing it in the browser with a library such as SheetJS (the xlsx package), which turns the binary .xlsx data into a workbook object with named sheets and cells. The shortest working route is: load the library, get the file from an <input type="file"> element, read it as an ArrayBuffer, pass that buffer to XLSX.read(), then convert the sheet you want with XLSX.utils.sheet_to_json(). On the server with Node.js the same library works, so you can read the file from disk instead of from a file picker. The steps below cover the browser case first because it is the most common, then note the Node.js differences.
What You'll Need
- A JavaScript project you can run in a browser, plus basic familiarity with the DOM and promises.
- The SheetJS library (
xlsxon npm, or a script build you download from the official SheetJS site). Use the official distribution only, not a random CDN copy you cannot verify. - An Excel file to test with — a simple
.xlsxwith a header row and a few data rows is easiest to debug. - For the Node.js route: a Node.js installation and npm.
- A browser with developer tools open so you can read console errors.
Step-by-Step
- Install or load the library. In a bundled project run
npm install xlsxand thenimport * as XLSX from 'xlsx';at the top of your script. In a plain HTML page you can include the official browser build with a<script>tag, which exposes a globalXLSXobject. Keep the version pinned so an update does not silently change behaviour. - Add a file input. Put
<input type="file" id="fileInput" accept=".xlsx,.xls,.csv">in your page. Theacceptattribute filters the picker to spreadsheet files but does not enforce anything — always validate the file you actually receive. - Read the chosen file into memory. Listen for the input's
changeevent, takeevent.target.files[0], and read it with aFileReader. Setreader.readAsArrayBuffer(file)and do the parsing insidereader.onload, usingreader.result. Reading as ArrayBuffer is the reliable choice for.xlsx, which is a zipped XML package rather than plain text. - Parse the buffer into a workbook. Call
const workbook = XLSX.read(reader.result, { type: 'array' });. The returnedworkbookholds every sheet, plus metadata. If you pass the wrongtypevalue you get an empty or garbled workbook, so match the type to how you read the file:arrayfor an ArrayBuffer,binaryfor a binary string,base64for base64 text. - Pick the sheet.
workbook.SheetNamesis an array of sheet names in order. Take the one you want, for exampleconst sheetName = workbook.SheetNames[0];, thenconst sheet = workbook.Sheets[sheetName];. Never assume the first sheet is the data sheet — check the names, or let the user choose. - Convert the sheet to something usable.
const rows = XLSX.utils.sheet_to_json(sheet);gives an array of objects keyed by the header row, which is usually what you want. Pass{ header: 1 }instead to get arrays of cells, or{ defval: '' }to fill blank cells with an empty string rather than leaving keys missing. - Validate and display the data. Check that
rows.lengthis greater than zero, confirm the column names you expect are present, and only then bind the data to your table or send it somewhere. Log a clear message when a required column is missing instead of letting undefined values spread through the app. - Handle errors. Wrap the parse in a
try/catchand show a readable message. A failed parse throws or returns an empty workbook when the file is corrupt, password-protected, or not actually a spreadsheet despite its extension. - For Node.js, swap the source. Install the same package, then
const XLSX = require('xlsx');, read the file withfs.readFileSync('data.xlsx'), and callXLSX.read(buffer, { type: 'buffer' }). The later steps — sheet names, sheet selection,sheet_to_json— are identical.
Common Mistakes
- Using the wrong read type. Parsing an ArrayBuffer as
binaryor a base64 string asarrayproduces confusing garbage or an empty sheet. - Assuming cell values are numbers. Formatted cells can come back as formatted text; dates in particular often arrive as serial numbers unless you read the formatted text or convert them explicitly.
- Trusting the file extension. Renaming a CSV to
.xlsxdoes not make it one. If users can upload anything, check the actual content before parsing. - Ignoring hidden rows or trailing blank rows. These can inflate your row count or shift header detection.
- Forgetting about encrypted files. Password-protected workbooks will not parse without the password, and the library will not guess one.
- Doing heavy parsing on the main thread for very large files. A large sheet can freeze the interface while it parses.
Tips
- Use
sheet_to_jsonwith an explicit header option when the file may have blank first rows, so the parser does not mistake a blank line for the header. - Read only the sheets you need rather than converting everything up front, especially in loops.
- Keep a small sample file in your project for testing, and test files with merged cells, blank rows and mixed types — those are where parsers surprise people.
- If you only need the values and not formatting, avoid converting to HTML or styled output, which is slower and harder to validate.
- For big files in the browser, consider doing the parse in a Web Worker so the page stays responsive; post the ArrayBuffer to the worker and return the rows.
- Strip leading and trailing spaces from header names before relying on them as object keys, since stray spaces break lookups.
Troubleshooting
If the workbook comes back with no sheets or sheet_to_json returns an empty array, first confirm the file is really an .xlsx (open it in Excel or LibreOffice and check it has data on a visible sheet). Next verify the type option passed to XLSX.read matches how you read the file, since this is the single most common cause. Then check that you are reading reader.result inside onload, not before it fires — reading too early gives an empty buffer.
If values are garbled or characters look wrong, check the file's encoding (relevant mainly for CSV) and make sure you are not reading binary data as a UTF-8 string. If numbers appear as text, look at the cell formatting in the source file and convert explicitly in code. If dates are wrong, treat the serial number and convert it, or use the formatted text.
If the parse throws, catch it and log the error; common causes are a corrupt download, a file truncated by a failed upload, or a password-protected sheet. Ask the user to re-export the file and try again. If nothing parses after a library update, check the changelog for a changed default and pin to the version that worked.
Frequently Asked Questions
Can I read Excel files in the browser without a server?
Yes. The parsing happens entirely in JavaScript on the client, and the file never needs to leave the device unless you choose to upload it.
Does the same code work for .xls files?
Yes. SheetJS reads the older binary .xls format as well as .xlsx and CSV; pass the same ArrayBuffer and use type: 'array'.
Why does my date column show a five-digit number?
Excel stores dates as serial numbers. Convert them with a date offset, or read the formatted cell text instead of the raw value.
Can I read password-protected workbooks?
Only if you supply the password to the parser; without it the workbook will not open. Do not attempt to crack protected files.
How do I get all sheets at once?
Loop over workbook.SheetNames and run sheet_to_json on each sheet, keeping the results keyed by sheet name.
Does it work in React or other frameworks?
Yes. The library is framework-agnostic; import it in a component or a separate module and handle the file input as you would in plain JavaScript.