JavaScript cannot double-click an Excel file the way Windows Explorer can. A browser has no direct file-system access, and the JS engine has no built-in Excel parser. What you actually do is one of two things: let the user pick a .xlsx file through an <input type="file"> and parse it in memory with a library such as SheetJS, or, in Node.js, read the file from disk and parse the buffer. The shortest working version in a browser is a file input, a CDN script tag for SheetJS, and a FileReader call that hands the binary data to XLSX.read(). From there, XLSX.utils.sheet_to_json() gives you an array of objects you can render, log or send to a server. Everything else in this guide is detail on that flow.
What You'll Need
- A plain HTML page with a script tag, or a bundler setup (Vite, webpack). No framework is required.
- SheetJS, officially distributed as the
xlsxpackage on npm and as a browser bundle from the project's own CDN or download page. Use the official build only. - For Node.js server-side parsing, Node 18 or newer and the same
xlsxpackage installed locally with npm. - A test
.xlsxfile with a header row. An.xlsfile works with the same library but you should test it separately, since older formats handle dates and formulas differently. - For legacy
.xlsfiles served from a web app, note that some very old variants may need extra code pages; the library's documentation covers this, and modern .xlsx files need no extras.
Step-by-Step
These steps are for the browser. Menu and API names are stable, but library versions differ, so check the SheetJS docs for the exact import style your build uses.
- Add the file input to your HTML. Create an input with
type="file"and anacceptattribute so the picker filters to spreadsheets:<input type="file" id="file" accept=".xlsx,.xls">. The accept value is a hint to the OS dialog; it does not stop a determined user from choosing another file, so validate the extension in code too. - Load the library. In a bundler project run
npm install xlsxand thenimport * as XLSX from 'xlsx';. In a plain page, add a script tag pointing at the official SheetJS browser build, then access the globalXLSXobject. Do not copy a random minified file from a forum. - Listen for the file selection. Attach a
changelistener to the input. Inside the handler readevent.target.files[0]. If it is undefined, the user cancelled the dialog, so return early instead of throwing. - Read the file as an ArrayBuffer. Use
file.arrayBuffer(), which returns a Promise, andawaitit. This is the modern replacement for the olderFileReaderplusreadAsArrayBufferpattern. Do not usereadAsTextor read the file as a string, because xlsx is a zipped binary container and text conversion corrupts it. - Parse the workbook. Pass the buffer to
XLSX.read(buffer, { type: 'array' }). The result is a workbook object whoseSheetNamesarray lists every tab in the file. Reading the first sheet by name withworkbook.Sheets[workbook.SheetNames[0]]is the usual next step. - Convert the sheet to usable data. Call
XLSX.utils.sheet_to_json(sheet)to get an array of objects keyed by the header row, or pass{ header: 1 }to get arrays of rows instead. The object form is easiest when row one contains column names and there are no merged cells. - Handle dates carefully. Excel stores dates as serial numbers. In the conversion options pass
{ cellDates: true }to the read call so date cells become JavaScript Date objects. Without it you get numbers like 45000 that mean nothing to a reader. - Do something with the result. Render the rows into a table, filter them, or POST the JSON to your backend. Keep the parsed data in a variable rather than re-reading the file on every interaction.
- Clear the input after processing. Set the input's value to an empty string so selecting the same file again fires a new
changeevent. This is a frequent source of confusion when testing. - Parse server-side if the file is on the server. In Node.js, read it with
fs.readFileSync(path)to get a Buffer and pass that toXLSX.read(buffer, { type: 'buffer' }). Never trust the path from user input, and never write uploaded files into a location that the web server serves directly.
Common Mistakes
- Reading the file as text. A .xlsx file is a ZIP archive; converting it to a UTF-8 string destroys the structure and the parse fails with an unhelpful error.
- Forgetting
awaitonfile.arrayBuffer(). You then hand a Promise to the parser instead of bytes. - Assuming row one is always a header. Files exported from other systems often have title rows, blank rows or merged banner cells above the real header. Pass
{ header: 1 }and inspect the first rows before mapping columns. - Expecting formulas to evaluate. The parser returns the last cached value stored in the file, not a live recalculation. If a file was saved by a tool that never calculated, cells can be empty.
- Trying to call an API that writes files directly to the user's disk from a normal page. Browsers do not allow silent disk writes; the alternative is generating a Blob and triggering a download the user must approve.
- Ignoring workbook size. Parsing a very large sheet blocks the main thread and freezes the tab. Read in a Web Worker if files can be big.
Tips
Keep the file input and the parse logic in one small function so it is easy to test with different files. Use XLSX.utils.sheet_to_json(sheet, { defval: '' }) so missing cells come back as empty strings rather than absent keys, which makes downstream code simpler. Validate columns by name after parsing instead of relying on position, because column order changes between exports. If you show the data to users, sanitize text before injecting it into the DOM; spreadsheet cells are untrusted input and can contain markup. For repeat uploads, show the file name and size next to the input so the user knows which file is loaded. When you only need one sheet, avoid iterating every sheet by name; read the sheet you need directly.
Troubleshooting
The parse throws immediately. Confirm the buffer is truly binary. Log the first bytes; a valid .xlsx starts with the ZIP signature PK. If you see readable text instead, you used a text read method.
The result is an empty array. The selected sheet is probably empty, or the range starts below your expected rows. Compare sheet['!ref'] with what you expected to get the actual used range.
Column names are wrong or shifted. The file has a title row or merged header. Inspect with header: 1, find the true header index, and slice rows before converting.
Dates show as numbers. Add cellDates: true to the read options, and be aware that times may still arrive as fractions of a day.
The change event never fires twice. You did not reset the input value after handling the first file.
Nothing happens at all. Open the browser console. A missing global, a 404 on the script, or a Content Security Policy blocking the CDN are the usual causes. Download the library and serve it locally if a policy blocks external scripts.
The tab freezes. The file is too large for the main thread. Move parsing into a Web Worker and post the result back.
Frequently Asked Questions
Can JavaScript open an Excel file without a library?
No. .xlsx is a zipped set of XML parts and the browser has no built-in parser for it. Plain JavaScript would have to unzip and parse the XML manually, which is exactly what libraries already solve.
Can a web page open a file from a local path automatically?
No. Browsers block silent access to local paths for security. The user must choose the file through a picker or drag and drop it onto the page.
Does this work with .csv files too?
Yes. SheetJS reads CSV as well, but you must pass the correct type option and watch for delimiter and encoding problems, which are common with files exported from regional settings.
Can I open an Excel file on a mobile browser?
Yes. The file input opens the phone's file picker, and the library runs in mobile browsers. Very large files are more likely to be slow or memory-constrained there.
Can the page save edits back to the workbook?
It can build a new workbook in memory and offer it as a download, but it cannot write over the original file in place. The user chooses where the download goes.
Why do some formulas show as errors?
The parser reads stored values, not recalculated results. If the file was last saved by software that did not compute formulas, those cells hold an error marker or nothing.