Free Node.js Library to Open & Read Excel Files
Open Source Node.js Excel Library that enables Software Developers to Parse and Read Data from Excel Files in Browser & Node.js Applications with ease.
Excel is still a common tool for storing and manipulating data in today's data-driven society. Software developers frequently need to incorporate the ability to read Excel files into their applications. While there are several libraries available for this purpose, it can be difficult to choose one that is reliable, easy to use, and kept up to date. A notable example of such a library is the open-source read-excel-file project. With just a few lines of code, this JavaScript library can parse and read data from Excel files inside Node.js environment.
Developed by catamphetamine, the read-excel-file is an open-source JavaScript package intended to make it easier to read Excel files in web and Node.js contexts. Developers can use the library for both server-side and client-side processing thanks to its dual compatibility. Because of the library's strength and portability, software developers can easily extract data from Excel spreadsheets. Its main selling points are its effectiveness and simplicity, which make it a great option for both inexperienced and seasoned developers.
The simplicity of the read-excel-file library is one of its main benefits. It is compatible with contemporary Excel documents and supports the most widely used Excel file types, XLS and XLSX. Large datasets may be handled by the library with ease, and it offers helpful error warnings and validation rules to guarantee the accuracy of the data being read. Additionally, it supports a few more sophisticated capabilities, including handling unique data types, including dates, and creating schemas to map Excel columns to particular data types and structures. Thanks to its robust community support and ongoing maintenance, it is a dependable alternative for developers across the globe.
Getting Started with read-excel-file
To install read-excel-file, you can use npm, the package manager for JavaScript. Please use the following commands for a successful installation.
Install read-excel-file via npm
npm install read-excel-file --save
You can download the compiled shared library from GitHub repository and install it.
Read Large Excel Files in Node.js Apps
Software developers often need to integrate Excel file reading capabilities into their applications. The open source read-excel-file library makes it easy for software developers to load and read small as well as large Excel files inside Node.js applications with just a couple of lines of code. It supports various file formats, including .xls and .xlsx, making it compatible with a wide range of Excel versions. Additionally, it handles parsing and formatting of data, ensuring that developers can easily access and manipulate the extracted information. The following basic example demonstrates how to read an Excel file in a Node.js environment using JavaScript commands.
How to Load and Read an Excel File inside Node.js Environment?
const readXlsxFile = require('read-excel-file/node');
// Path to your Excel file
const filePath = 'path/to/your/excel-file.xlsx';
readXlsxFile(filePath).then((rows) => {
// rows is an array of arrays representing the rows and columns of the Excel file
console.log(rows);
}).catch((error) => {
console.error('Error reading Excel file:', error);
});
Flexible Data Parsing in Node.js
The open source read-excel-file library has included complete support for flexible data parsing, enabling software developers to define custom schemas to interpret the data. This feature is particularly useful when dealing with complex Excel files that contain various data types and structures. By defining a schema, you can ensure that the data is parsed correctly and consistently. For more advanced scenarios, you can define a schema to map the Excel columns to specific data types and structures. The following example shows how software developers can define a custom schema to validate the structure and content of Excel files inside Node.js applications.
How to Parse Data of an Excel File by Defining Custom Schema inside Node.js?
const schema = {
'Column 1': {
prop: 'column1',
type: String,
required: true
},
'Column 2': {
prop: 'column2',
type: Number
}
};
readXlsxFile(filePath, { schema }).then(({ rows, errors }) => {
if (errors.length > 0) {
console.error('Validation errors:', errors);
} else {
console.log(rows);
}
});
Custom Types and Date Parsing in Node.js
The read-excel-file library provides robust support for custom types and date parsing, which is especially useful when dealing with complex data structures in Excel files. This is a very useful feature and allows software developers to define how different data types, including dates, should be parsed, ensuring data integrity and consistency. To define a custom schema, you create an object where the keys are the column names from the Excel file, and the values describe the properties, types, and validation rules for those columns. In the below example the 'Event Date' column values are parsed into Date objects, and the toISOString method is used to format the dates as ISO strings.
How to Handel Date Parsing in Excel XLSX Worksheet inside Node.js Apps?
const readXlsxFile = require('read-excel-file/node');
// Schema with date parsing
const schema = {
'Event Date': {
prop: 'eventDate',
type: Date,
required: true
}
};
// Read and parse the Excel file with date parsing
readXlsxFile('path/to/file.xlsx', { schema }).then(({ rows, errors }) => {
if (errors.length > 0) {
console.error('Validation errors:', errors);
} else {
rows.forEach(row => {
console.log(`Event Date: ${row.eventDate.toISOString()}`);
});
}
}).catch((error) => {
console.error('Error reading Excel file:', error);
});
Better Error Handling Support
Robust error handling features are incorporated in the open-source read-excel-file package. Validation issues that arise while parsing are recorded and can be addressed or logged appropriately. To guarantee the integrity of the data being read, the library includes validation rules and offers clear error signals. This makes it easier for developers to find and fix problems with the data or the Excel file itself more rapidly.