Introduction to node-xlsx
node-xlsx is a powerful and versatile library for handling Excel files in Node.js. It provides a simple and efficient way to read, write, and manipulate Excel files without requiring Excel software. This library is ideal for developers who need to process large amounts of data in Excel format.
Key Features and APIs of node-xlsx
Here are some of the most useful APIs provided by the node-xlsx library, complete with code snippets to get you started.
1. Installing node-xlsx
npm install node-xlsx
2. Reading Excel Files
The following code snippet demonstrates how to read data from an Excel file:
const xlsx = require('node-xlsx').default;
const fs = require('fs');
const workSheetsFromBuffer = xlsx.parse(fs.readFileSync(`${__dirname}/file.xlsx`));
const workSheetsFromFile = xlsx.parse(`${__dirname}/file.xlsx`);
console.log(workSheetsFromFile);
3. Writing Excel Files
Writing data to an Excel file is straightforward with node-xlsx:
const xlsx = require('node-xlsx').default;
const fs = require('fs');
const data = [
["S/N", "Name", "Age"],
[1, "John Doe", 28],
[2, "Jane Doe", 32]
];
const buffer = xlsx.build([{name: "Sheet1", data: data}]);
fs.writeFileSync(`${__dirname}/output.xlsx`, buffer);
4. Adding Multiple Sheets
Creating a workbook with multiple sheets is seamless. Here is an example:
const xlsx = require('node-xlsx').default;
const fs = require('fs');
const workSheets = [
{name: "Sheet1", data: [["Name", "Age"], ["John", 30]]},
{name: "Sheet2", data: [["Product", "Price"], ["Apple", "$2"]]}
];
const buffer = xlsx.build(workSheets);
fs.writeFileSync(`${__dirname}/multiple_sheets.xlsx`, buffer);
5. Parsing Excel Files with Options
Using options to fine-tune the parsing process:
const xlsx = require('node-xlsx').default;
const fs = require('fs');
const options = {cellDates: true, dateNF: 'mm/dd/yyyy'};
const workSheetsFromFile = xlsx.parse(`${__dirname}/file.xlsx`, options);
console.log(workSheetsFromFile);
Example Application: Employee Data Management
Below is a simple application to manage employee data using the APIs discussed above:
const xlsx = require('node-xlsx').default;
const fs = require('fs');
// Read existing employee data
const getEmployees = () => {
const workSheetsFromFile = xlsx.parse(`${__dirname}/employees.xlsx`);
return workSheetsFromFile[0].data;
};
// Add a new employee
const addEmployee = (name, age) => {
let employees = getEmployees();
employees.push([employees.length, name, age]);
const buffer = xlsx.build([{name: "Employees", data: employees}]);
fs.writeFileSync(`${__dirname}/employees.xlsx`, buffer);
console.log('Employee added successfully!');
};
// List all employees
const listEmployees = () => {
const employees = getEmployees();
console.log('Employee List:');
employees.forEach(emp => {
console.log(`ID: ${emp[0]} | Name: ${emp[1]} | Age: ${emp[2]}`);
});
};
// Example usage
addEmployee("Alice Brown", 29);
listEmployees();
With these examples, you can start integrating Excel file handling into your Node.js applications effortlessly using node-xlsx.
Hash: a1a9ac13efba05fb59af12b231315a074329d4fe632ec88681376deca0a90c48