Keywords: Node.js | Excel file creation | excel4node | SheetJS | data export
Abstract: This article provides a comprehensive overview of various methods for creating Excel files in Node.js environments, with detailed analysis of excel4node and xlsx libraries. Through complete code examples and comparative analysis, it helps developers choose the most suitable solution for their projects, covering the entire implementation process from basic data writing to complex style settings.
Fundamental Principles of Excel File Creation
Creating Excel files in Node.js environments primarily involves two technical approaches: using specialized Excel manipulation libraries or leveraging simple text format conversion. Excel files are essentially compressed package formats based on XML (for .xlsx), containing multiple XML files that describe worksheets, styles, and data.
Detailed Usage of excel4node Library
excel4node is a Node.js library developed based on the official Excel specification, providing comprehensive Excel file creation capabilities. The library supports creating workbooks, worksheets, setting cell styles, adding formulas, and other advanced features.
// Require excel4node library
var excel = require('excel4node');
// Create a new instance of a Workbook class
var workbook = new excel.Workbook();
// Add Worksheets to the workbook
var worksheet = workbook.addWorksheet('Sheet 1');
var worksheet2 = workbook.addWorksheet('Sheet 2');
// Create a reusable style
var style = workbook.createStyle({
font: {
color: '#FF0800',
size: 12
},
numberFormat: '$#,##0.00; ($#,##0.00); -'
});
// Set value of cell A1 to 100 as a number type styled with parameters of style
worksheet.cell(1,1).number(100).style(style);
// Set value of cell B1 to 200 as a number type styled with parameters of style
worksheet.cell(1,2).number(200).style(style);
// Set value of cell C1 to a formula styled with parameters of style
worksheet.cell(1,3).formula('A1 + B1').style(style);
// Set value of cell A2 to 'string' styled with parameters of style
worksheet.cell(2,1).string('string').style(style);
// Set value of cell A3 to true as a boolean type styled with parameters of style
worksheet.cell(3,1).bool(true).style(style).style({font: {size: 14}});
// Write workbook to file
workbook.write('Excel.xlsx');
Alternative Solution: SheetJS (xlsx) Library
SheetJS (commonly used through the xlsx package) is another popular Excel processing library with higher download counts and community activity. This library supports both reading and writing Excel files, offering comprehensive functionality and excellent performance.
const XLSX = require('xlsx');
/* create a new blank workbook */
const workbook = XLSX.utils.book_new();
// Do stuff, write data
//
//
// write the workbook object to a file
XLSX.writeFile(workbook, 'out.xlsx');
Simple Text Format Conversion Method
For basic data export requirements, tab-separated text files saved with .xls extension can be used. While this method has limited functionality, it's simple to implement and suitable for basic data export scenarios.
var fs = require('fs');
var writeStream = fs.createWriteStream("file.xls");
var header="Sl No"+"\t"+" Age"+"\t"+"Name"+"\n";
var row1 = "0"+"\t"+" 21"+"\t"+"Rob"+"\n";
var row2 = "1"+"\t"+" 22"+"\t"+"bob"+"\n";
writeStream.write(header);
writeStream.write(row1);
writeStream.write(row2);
writeStream.close();
Library Selection Recommendations and Performance Considerations
When choosing an Excel processing library, consider specific project requirements: excel4node is developed based on official specifications with complete functionality but relatively less maintenance; SheetJS has active community support and handles both read and write operations, making it more suitable for complex Excel processing scenarios; simple text conversion methods work for basic data exports but have limited capabilities.
Cross-Platform Compatibility Considerations
All mentioned solutions fully support Linux server environments without requiring additional configuration or dependencies. Node.js's cross-platform nature ensures code consistency across different operating systems.