Read .xlsx files in a browser or Node.js.
It also supports parsing spreadsheet rows into JSON objects using a schema.
Also check out write-excel-file for writing .xlsx files.
Migrating from 6.x to 7.x
- Renamed the default export
"read-excel-file"to"read-excel-file/browser", and it uses Web Workers now.- Old:
import readExcelFile from "read-excel-file" - New:
import readExcelFile from "read-excel-file/browser"
- Old:
- The minimum required Node.js version is 18.
Migrating from 7.x to 8.x
-
If you were using the default exported function:
- Renamed the default exported function to a named exported function
readSheet.- Old:
import readExcelFile from "read-excel-file/browser" - New:
import { readSheet } from "read-excel-file/browser" - And same for other exports like
"read-excel-file/node", etc.
- Old:
- The default exported function now returns a different kind of result. Specifically, now it returns all available sheets — an array of objects:
[{ sheet: "Sheet 1", data: [['a1','b1','c1'],['a2','b2','c2']] }, ...]. - The default exported function used to return sheet names when passed
getSheets: trueparameter. Now, instead of that, the default exported function just returns all available sheets, from which one could get the sheet names.
- Renamed the default exported function to a named exported function
-
If you were using
readSheetNames()function:- Removed exported function
readSheetNames(). Use the default exported function instead. The default exported function now returns all sheets.
- Removed exported function
-
If you were using
parseExcelDate()function:- Removed exported function
parseExcelDate()because there seems to be no need to have it exported.
- Removed exported function
-
If you were using
schemaparameter:- Removed
schemaparameter. Instead, use exported functionparseData(data, schema)to map data to an array of objects.- Old:
import readXlsxFile from "read-excel-file"and thenconst { rows, errors } = await readXlsxFile(..., { schema }) - New:
import { readSheet, parseData } from "read-excel-file/browser"and thenconst result = parseData(await readSheet(...), schema)- The
resultof the function is an array where each element represents a "data row" and has shape{ object, errors }.- Depending on whether there were any errors when parsing a given "data row", either
objectorerrorsproperty will beundefined. - The
errorsdon't have arowproperty anymore because it could be derived from "data row" number.- In version
9.x, therowproperty has been re-added, so consider migrating straight to9.x.
- In version
- In version
9.x, the returned result ofparseData()has been changed back to{ errors, objects }, so consider migrating straight to9.x. In that case, if there're no errors,errorswill beundefined; otherwise,errorswill be a non-empty array andobjectswill beundefined.
- Depending on whether there were any errors when parsing a given "data row", either
- The
- Old:
- Renamed some
schema-related parameters:schemaPropertyValueForMissingColumn→propertyValueWhenColumnIsMissingschemaPropertyValueForMissingValue→propertyValueWhenCellIsEmptyschemaPropertyShouldSkipRequiredValidationForMissingColumn→ (removed)getEmptyObjectValue→transformEmptyObject- The leading
.character is now removed from thepathparameter.
- The leading
getEmptyArrayValue→transformEmptyArray- The leading
.character is now removed from thepathparameter.
- The leading
- Previously, when using a
schemato parse comma-separated values, it used to ignore any commas that're surrounded by quotes, similar to how it's done in.csvfiles. Now it no longer does that. - Previously, when using a
schemato parse comma-separated values, it used to allow empty-string elements. Now it no longer does that and such empty-string elements will now result in an error with properties:{ error: "invalid", reason: "syntax" }. - Previously, when using a
schemato parsetype: Dateproperties, it used to support bothDateobjects and numeric timestamps as the input data for the property value. In the latter case, it simply force-converted those numeric timestamps to correspondingDateobjects. NowparseData()function no longer does that, and demands the input data fortype: Dateschema properties to only beDateobjects, i.e. it shifts the responsibility to interpret date cell values correctly ontoreadSheet()andreadExcelFile()functions. And I'd personally assume that in any real-world (i.e. non-contrived) scenario those functions would interpret date cell values correctly, so I personally don't consider this a "breaking change". Still, formally, it is a "breaking change" and therefore should be mentioned. So if, for some strange reason, those two functions happen to not recognize a date cell value correctly,parseData()function will return an error for such cell:"not_a_date". - Previously, when using a
schemato parse sheet data, and a given row of data was completely empty, it didn't run anyrequiredproperty validations. Now it no longer does that and it will run allrequiredproperty validations regardless of whether it's a completely empty row of data or not.
- Removed
-
If you were using
transformDataparameter:- Removed
transformDataparameter because theschemaparameter was extracted into a separate function calledparseData(). Now, if required, a developer could transform thedatamanually and then pass it toparseData()function.
- Removed
-
If you were using
isColumnOrientedparameter:- Removed
isColumnOrientedparameter because it seemed to be of no use.
- Removed
-
If you were using
ignoreEmptyRowsparameter:- Removed
ignoreEmptyRowsparameter. PassingignoreEmptyRows: trueparameter no longer makes it skip empty rows in the middle of a sheet. Now it's always the default behavior, as it used to be: only empty rows at the end of a sheet are ignored.
- Removed
-
If you were using TypeScript:
- Renamed some of the exported types:
Type→ParseDataCustomTypeErrororSchemaParseCellValueError→ParseDataErrorCellValueRequiredError→ParseDataValueRequiredErrorParsedObjectsResult→ParseDataResult
- Renamed some of the exported types:
Migrating from 8.x to 9.x
- If you were using
parseData()function:- Rewrote the code of the
parseData()function and renamed it toparseSheetData(). - The result of
parseSheetData()function is now{ errors, objects }. If there're no errors,errorswill beundefined. Otherwise,errorswill be a non-empty array andobjectswill beundefined.- Previously the result of
parseSheetData()function was[{ errors, object }, ...], i.e. theerrorswere split between each particular data row. Now theerrorsare combined for all data rows. The rationale is that it's simpler to handle the result of the function this way. - Re-added
row: numberproperty to theerrorobject. It's the number of the data row that caused the error, starting from1. - Added
columnIndex: numberproperty to theerrorobject.
- Previously the result of
- Renamed some of the exported TypeScript types:
ParseDataCustomType→ParseSheetDataCustomTypeParseDataCustomTypeErrorMessage→ParseSheetDataCustomTypeErrorMessageParseDataCustomTypeErrorReason→ParseSheetDataCustomTypeErrorReasonParseDataError→ParseSheetDataErrorParseDataValueRequiredError→ParseSheetDataValueRequiredErrorParseDataResult→ParseSheetDataResult
- In a
schema, a nested object could be declared as:{ required: true/false, schema: { ... } }. This is still true but therequiredflag is now only allowed to be eitherundefinedorfalse, sotruevalue is not allowed. The reason is quite simple. If a nested object as a whole is marked asrequired: true, and then it happens to be empty, a"required"error should be returned for it. But that error would also have to include acolumntitle, and a nested object simply can't be pinned down to a single column in a sheet because it is by definition spread over multiple columns. So instead of marking a nested object as a whole withrequired: true, mark the specific required properties of it.
- Rewrote the code of the
npm install read-excel-file --saveAlternatively, it could be included on a web page directly via a <script/> tag.
If your .xlsx file only has a single "sheet", or if you only need to read a single "sheet", or if you don't care what a "sheet" is, use readSheet() function.
For example, consider the following .xlsx file:
| Name | Date of Birth | Married | Kids |
|---|---|---|---|
| John Smith | 1/1/1995 | TRUE | 3 |
| Kate Brown | 3/1/2010 | FALSE | 0 |
Here's how to read it using readSheet() function:
import { readSheet } from 'read-excel-file/node'
await readSheet(file) ===
[
['Name', 'Date of Birth', 'Married', 'Kids'],
['John Smith', 1995-01-01T00:00:00.000Z, true, 3],
['Kate Brown', 2010-03-01T00:00:00.000Z, false, 0]
]The result is an array of rows. Each row is an array of values — string, number, boolean or Date.
It also has an optional second argument — sheet — which could be a sheet number (starting from 1) or a sheet name. By default, it reads the first sheet.
But if you need to read all available "sheets" in a file, use the default exported function:
import readExcelFile from 'read-excel-file/node'
await readExcelFile(file) ===
[{
sheet: 'Sheet1',
data: [
['Name', 'Age'],
['John Smith', 30],
['Kate Brown', 15]
]
}, {
sheet: 'Sheet2',
data: ...
}]The result is a non-empty array of "sheets". Each "sheet" is an object with properties:
sheet— Sheet name.- Example:
"Sheet1"
- Example:
data— Sheet data. An array of rows. Each row is an array of values —string,number,booleanorDate.- Example:
[ ['Name','Age'], ['John Smith',30], ['Kate Brown',15] ]
- Example:
This package provides a separate import path for each different environment, as described below.
read-excel-file/browser
It can read from a File, a Blob or an ArrayBuffer.
Example: User chooses a file and the web application reads it.
<input type="file" id="input" />import { readSheet } from 'read-excel-file/browser'
const input = document.getElementById('input')
input.addEventListener('change', () => {
const data = await readSheet(input.files[0])
})Note: Internet Explorer 11 is an old browser that doesn't support Promise, and hence requires a polyfill.
Example 2: Reading from a URL
const response = await fetch('https://example.com/spreadsheet.xlsx')
const block = await response.blob()
const data = await readSheet(blob)Example 3: Using read-excel-file in a Web Worker
All exports of read-excel-file already use a Web Worker under the hood when reading .xlsx file contents. This is in order to avoid freezing the UI when reading large files. So using an additional Web Worker on top of that isn't really necessary. Still, for those who require it, this example shows how a user chooses a file and the web application reads it in a Web Worker using read-excel-file/web-worker import path.
// Step 1: Initialize Web Worker.
const worker = new Worker('web-worker.js')
worker.onmessage = function(event) {
// `event.data` is a `File`.
console.log(event.data)
}
worker.onerror = function(event) {
console.error(event.message)
}
// Step 2: User chooses a file and the application sends it to the Web Worker.
const input = document.getElementById('input')
input.addEventListener('change', () => {
worker.postMessage(input.files[0])
})import { readSheet } from 'read-excel-file/web-worker'
onmessage = async function(event) {
const sheetData = await readSheet(event.data)
postMessage(sheetData)
}read-excel-file/node
It can read from a file path, a Stream, a Buffer or a Blob.
Example 1: Read from a file path.
import { readSheet } from 'read-excel-file/node'
const data = await readSheet('/path/to/file')Example 2: Read from a Stream
import { readSheet } from 'read-excel-file/node'
const data = await readSheet(fs.createReadStream('/path/to/file'))read-excel-file/universal
This one works both in a web browser and Node.js. It can only read from a Blob or an ArrayBuffer, which could be a bit less convenient for general use.
import { readSheet } from 'read-excel-file/universal'
const data = await readSheet(blob)By default, it automatically trims all string values. To disable this behavior, pass trim: false option.
readExcelFile(file, { trim: false }).xlsx file format originally had no dedicated "date" type, so dates are in almost all cases stored simply as numbers, equal to the count of days since 01/01/1900 (with a few quirks). To correctly interpret such numbers as dates, each date cell in an .xlsx file specifies a certain "format" — for example, "d mmm yyyy" — that instructs a spreadsheet viewer application to interpret the numeric value in the cell as a date rather than a number, and display it using the specified format.
Being no different from a generic spreadsheet viewer application, this package follows the same practice: it attempts to guess whether a given cell value is a date or a number by looking at the cell's "format" — if the "format" is one of the known standard date formats then the cell value is interpreted as a date rather than a number. So usually there's no need to configure anything and it usually "just works" out-of-the-box.
Although there's still a possibility for an .xlsx file to specify a totally-custom non-standard date format. In such case, a developer could pass a dateFormat parameter to tell this package to parse cells having that specific "format" as date ones rather than numeric ones: readExcelFile(file, { dateFormat: 'mm/dd/yyyy' }).
When reading an .xlsx file, any numeric values are parsed from a string to a javascript number. But there's an inherent issue with javascript numbers in general — their floating-point precision is sometimes less than ideal. For example, 0.1 + 0.2 != 0.3. Yet, applications in areas such as finance or banking usually require 100% floating-point precision, which is usually worked around by using a custom implementation of a "decimal" data type such as decimal.js.
This package supports passing a custom parseNumber(string) function as an option when reading an .xlsx file. By default, it parses a string to a javascript number, but one could pass any custom implementation.
Example: Use "decimal" data type to perform further calculations on fractional numbers with 100% precision.
import Decimal from 'decimal.js'
readExcelFile(file, {
parseNumber: (string) => new Decimal(string)
})This package doesn't support reading cells that use formulas to calculate the value: SUM, AVERAGE, etc.
Here're the results of reading sample .xlsx files of different size:
| File Size | Browser | Node.js |
|---|---|---|
| 1 MB | 0.2 sec. | 0.25 sec. |
| 10 MB | 1.5 sec. | 2 sec. |
| 50 MB | 8.5 sec. | 14 sec. |
Oftentimes, the task is not just to read the "raw" spreadsheet data but also to convert each row of that data to a JSON object having a certain structure. Because it's such a common task, this package exports a named function parseSheetData(data, schema) which does exactly that. It parses sheet data into an array of JSON objects according to a pre-defined schema which describes how should a row of data be converted to a JSON object.
import { readSheet, parseSheetData } from "read-excel-file/browser"
const data = await readSheet(file)
const schema = { ... }
const { objects, errors } = parseSheetData(data, schema)
if (errors) {
console.error(errors)
} else {
console.log(objects)
}The parseSheetData() function returns an object — { objects, errors }. Depending on whether there were any errors when parsing the data, either objects or errors property will be undefined.
The sheet data that is being parsed should adhere to a simple structure: the first row should be a header row with just column titles, and each following row should specify the values for those columns.
The schema argument should describe the structure of the resulting JSON objects. An example of a schema is provided at the end of this section.
Specifically, a schema should be an object having the same keys as a resulting JSON object, with values being nested objects having the following properties:
column— The title of the column to read the value from.- If the column is missing from the spreadsheet, the property value will be
undefined.- This can be overridden by passing
propertyValueWhenColumnIsMissingoption. Isundefinedby default.
- This can be overridden by passing
- If the column is present in the spreadsheet but is empty, the property value will be
null.- This can be overridden by passing
propertyValueWhenCellIsEmptyoption. Isnullby default.
- This can be overridden by passing
- If the column is missing from the spreadsheet, the property value will be
required— (optional) Is the value required?- Could be one of:
required: booleantrue— The column must not be missing from the spreadsheet and the cell value must not be empty.false— The column can be missing from the spreadsheet, or the cell value can be empty.
required: (object) => boolean— A function returningtrueorfalsedepending on the other properties of the object.
- Could be one of:
validate(value)— (optional) Validates the value. Is only called for non-empty cells. If the value is invalid, this function should throw an error.schema— (optional) If the value is going to be a nested object,schemashould describe that nested object.- If when parsing such nested object, all of its property values happen to be empty —
undefinedornull— then the nested object will be itself set tonull.- This can be overridden by passing
transformEmptyObject(object, { path? })function as an option. By default, it returnsnull. - This applies both to nested objects and to the top-level object itself.
- This can be overridden by passing
- If when parsing such nested object, all of its property values happen to be empty —
type— (optional) If the value is not going to be a nested object,typeshould describe the type of the value. It will determine how the cell value will be converted to a property value. If notypeis specified then the property value will be same as the cell value.- Valid
types:- Standard types:
StringNumberBooleanDate
- One of the "utility" types that're exported from this package:
IntegerEmailURL
- Custom type:
- A function that receives a cell value and returns any kind of a parsed value. Returning
undefinedwill have same effect as returningnull. If the value is invalid, it should throw an error.
- A function that receives a cell value and returns any kind of a parsed value. Returning
- Standard types:
- If the cell value is comprised of comma-separated values (example:
"a, b, c") and if it should be parsed as an array of such values, then the propertytypecould be specified as an array —type: [elementType]— whereelementTypecould be any validtypedescribed above. For example, if a property is defined as{ type: [String] }and the cell value is"a, b, c"then the property value will be parsed as["a", "b", "c"].- If the cell is empty, or if every element of the parsed array is
nullorundefined, then the property value itself will be set tonull.- This can be overridden by passing
transformEmptyArray(array, { path })function as an option. By default, it returnsnull.
- This can be overridden by passing
- The separator could be specified by passing
arrayValueSeparatoroption. By default, it's",". - The separated parts of a cell value will be trimmed.
- If the cell is empty, or if every element of the parsed array is
- Valid
If there're any errors during the conversion process, the errors property returned from the function will be a non-empty array (by default, it's an empty array). Each error object has properties:
error: string— The error code. Examples:"required","invalid".- If a custom
validate()function is defined and it throws anew Error(message)then theerrorproperty will be the same as themessageargument. - If a custom
type()function is defined and it throws anew Error(message)then theerrorproperty will be the same as themessageargument.
- If a custom
reason?: string— An optional secondary error code providing more details about the error. I.e. "error.errorhappened specifically because oferror.reason". Currently, it could only be returned for the standardtypes.- Example:
{ error: "invalid", reason: "not_a_number" }for atype: Numberproperty means that "the cell value is invalid because it's not a number".
- Example:
row: number— The row number, starting from1.row: 1means "first row of data", etc.- Don't mind the header row.
column: string— The column title.columnIndex: number— The column index.columnIndex: 0means "first column", etc.
value?: any— The cell value.type?: any— Thetypeof the property, as defined by theschema.
Example:
// An example .xlsx document:
// --------------------------------------------------------------------------------------------------------
// | START DATE | SEATS | STATUS | CONTACT | COURSE TITLE | COURSE CATEGORY | COURSE IS FREE |
// --------------------------------------------------------------------------------------------------------
// | 03/24/2018 | 10 | SCHEDULED | (123) 456-7890 | Basic Algebra | Math, Arithmetic | TRUE |
// --------------------------------------------------------------------------------------------------------
const schema = {
startDate: {
column: 'START DATE',
type: Date
},
seats: {
column: 'SEATS',
type: Number,
required: true
},
status: {
column: 'STATUS',
type: String,
// An example of using `oneOf`
oneOf: [
'SCHEDULED',
'STARTED',
'FINISHED'
]
},
contact: {
column: 'CONTACT',
required: true,
// An example of using a custom `type`
type: PhoneNumber
},
// Nested object example
course: {
// A nested object could be declared as completely optional by specifying `required: false`.
// In that case, when all of its properties are missing from the input data, it wouldn't throw any error
// regardless of whether some of its properties are declared as `required: true` or not.
required: false,
schema: {
title: {
column: 'COURSE TITLE',
type: String,
// When course data is present, the course title must be specified.
required: true
},
categories: {
column: 'COURSE CATEGORY',
// An example of parsing comma-separated values.
type: [String]
},
isFree: {
column: 'COURSE IS FREE',
type: Boolean
}
}
}
}
// If this code was written in TypeScript, `schema` would've been declared as:
// const schema: Schema<Object, ColumnTitle> = { ... }
// Read `data` from an `.xlsx` file
const data = await readSheet(file)
// Parse `data` using a `schema`
const { objects, errors } = parseSheetData(data, schema)
// There have been no errors when parsing the sheet data, so `errors` is `undefined`.
// Should there have been any errors when parsing the sheet data, `errors` would've been
// an array of items having shape: `{ row, column, error, reason?, value?, type? }`.
errors === undefined
// There's one data row in the `.xlsx` file.
objects.length === 1
// The parsed data row.
objects[0] === {
startDate: new Date(Date.UTC(2018, 3 - 1, 24)),
seats: 10,
status: 'SCHEDULED',
contact: '+11234567890',
course: {
title: 'Basic Algebra',
categories: ['Math', 'Arithmetic']
isFree: true
}
}
// An example of a custom `type` parser function.
// It will parse the cell value when it's not empty.
function PhoneNumber(value) {
const number = parsePhoneNumber(value)
if (!number) {
throw new Error('invalid')
}
return number
}An example of defining a custom type in TypeScript
import type {
Schema,
CellValue,
ParseSheetDataError,
ParseSheetDataCustomType,
ParseSheetDataCustomTypeErrorMessage
} from 'read-excel-file/node'
type ColumnTitle = 'COLUMN TITLE 1' | 'COLUMN TITLE 2'
type CustomTypeValue = string
function CustomType(value: CellValue): CustomTypeValue {
if (typeof value !== 'string') {
throw new Error('not_a_string')
}
return '~' + value + '~'
}
type CustomTypeErrorMessage<Type extends ParseSheetDataCustomType<unknown>> =
Type extends typeof CustomType
? 'not_a_string'
: never
// type CustomTypeErrorReason<
// Type extends ParseSheetDataCustomType<unknown>,
// ErrorMessage extends ParseSheetDataCustomTypeErrorMessage<Type>
// > =
// Type extends typeof CustomType
// ? (ErrorMessage extends 'not_a_string' ? undefined : never)
// : never
type PossibleError = ParseSheetDataError<
ColumnTitle,
typeof CustomType,
CustomTypeErrorMessage<typeof CustomType>
// CustomTypeErrorReason<typeof CustomType, CustomTypeErrorMessage<typeof CustomType>>
>
interface Object {
property1: CustomTypeValue;
property2?: string;
}
const schema: Schema<Object, ColumnTitle> = {
property1: {
column: 'COLUMN TITLE 1',
type: CustomType,
required: true
},
property2: {
column: 'COLUMN TITLE 2',
type: String
}
}
const { objects, errors } = parseSheetData<Object, ColumnTitle, PossibleError>([
['COLUMN TITLE 1', 'COLUMN TITLE 2'],
['Value 1', 'Value 2']
], schema)
if (errors) {
for (const error of errors) {
console.error('Error in data row', error.row, 'column', error.column, ':', error.error, error.reason || '')
}
} else {
console.log('Objects', objects)
}An example of a React component to output errors
function ErrorsList({ errors }) {
return (
<ul>
{errors.map((error, i) => (
<li key={i}>
<ErrorItem error={error}>
</li>
))}
</ul>
)
}
function ErrorItem({ error }) {
const {
error: errorMessage,
reason,
row,
column,
columnIndex,
value,
type
} = error
// Error summary.
return (
<div>
<code>"{errorMessage}"</code>
{reason && ' '}
{reason && <code>("{reason}")</code>}
{' for value '}
<code>{stringifyValue(value)}</code>
{' in column '}
<code>"{column}"</code>
{' in data row '}
<code>{row}</code>
{' of the spreadsheet'}
</div>
)
}
function stringifyValue(value) {
// Wrap strings in quotes.
if (typeof value === 'string') {
return '"' + value + '"'
}
return String(value)
}An .xlsx file is just a .zip archive with an .xslx file extension. This package uses fflate for .zip decompression. See fflate's browser support for further details.
To include this library directly via a <script/> tag on a page, one can use any npm CDN service, e.g. unpkg.com or jsdelivr.com
<script src="https://unpkg.com/read-excel-file@5.x/bundle/read-excel-file.min.js"></script>
<script>
var input = document.getElementById('input')
input.addEventListener('change', function() {
readXlsxFile(input.files[0]).then(function(rows) {
// `rows` is an array of rows
// each row being an array of cells.
})
})
</script>On March 9th, 2020, GitHub, Inc. silently banned my account (erasing all my repos, issues and comments, even in my employer's private repos) without any notice or explanation. Because of that, all source codes had to be promptly moved to GitLab. The GitHub repo is now only used as a backup (you can star the repo there too), and the primary repo is now the GitLab one. Issues can be reported in any repo.