کتابخانه منبع باز Node.js برای نوشتن دادهها در فایلهای اکسل
یک کتابخانه اختصاصی منبع باز Node.js برای اکسل XLSX که API سادهای برای تولید فایلهای .xlsx (Excel) و نوشتن دادهها در فایلهای اکسل بهصورت استریمینگ ارائه میدهد.
Node-XLSX-Writer چیست؟
In the world of Node.js development, generating reports and exporting data are common requirements. While powerful libraries like exceljs exist, sometimes you need a solution that is laser‑focused on one task; writing XLSX files quickly and efficiently, without the overhead of reading or manipulating existing files. Enter Node‑XLSX‑Writer, a lightweight and straightforward library designed precisely for this purpose. The library stands out for its simplicity and focused functionality. Unlike comprehensive Excel libraries that handle reading, writing, and manipulation, this module concentrates solely on writing XLSX files, making it lightweight, easy to learn, fast implementation and reliable.
Node‑XLSX‑Writer is a lightweight and straightforward library designed specifically for generating XLSX (Excel) files in Node.js applications. For tasks like generating CSV‑like exports with the added benefits of Excel’s formatting and multi‑sheet support (through its straightforward API), it is an excellent choice that will keep your code clean and your application’s memory footprint small. Created by Ruben Vermeersch and reverse‑engineered from sample XLSX files, this module provides developers with a simple yet effective way to export data into Excel format without the complexity of larger Excel manipulation libraries. This makes it an ideal choice for applications that need to generate large reports from databases or APIs, as its streaming nature ensures low memory usage even with massive datasets.
شروع کار با Node-XLSX-Write
پیش از نصب Node-XLSX-Writer، باید کتابخانههای توسعه libzip بر روی سیستم شما نصب شده باشند. این کتابخانه به این بایندینگهای بومی برای ایجاد فایلهای zip (فایلهای XLSX در واقع XML فشردهشده هستند) وابسته است. لطفاً از دستورات زیر برای نصب موفق استفاده کنید.
Install Node‑XLSX‑Write via npm
npm install --save xlsx-writerشما میتوانید دانلود کنید کتابخانهٔ اشتراکی کامپایلشده را از مخزن GitHub و آن را نصب کنید.
ایجاد صفحهگسترده Excel XLSX در Node.js
کتابخانهٔ منبع باز Node-XLSX-Writer شامل ویژگیهایی برای ایجاد و مدیریت فایلهای Excel XLSX در داخل برنامههای Node.js است. این کتابخانه نیازی به Microsoft Excel یا COM interop ندارد. این کتابخانه فایل XLSX را (که در واقع یک فایل ZIP از بخشهای XML است) بهصورت برنامهنویسی میسازد. این ویژگی آن را برای استفاده سمت سرور Node.js مناسب میکند. مثال کد زیر سادهترین روش برای ایجاد یک فایل Excel با برخی دادهها را نشان میدهد.
How to Create Excel XLSX File inside Node.js Apps?
// Import the library
const XLSXWriter = require('node-xlsx-writer');
// 1. Create a new instance, specifying the output file path.
const xlsx = new XLSXWriter('./output/simple_report.xlsx');
// 2. Write the header row (optional, but recommended).
xlsx.writeRow(['Name', 'Email', 'Sign-Up Date']);
// 3. Write some data rows.
xlsx.writeRow(['Alice Smith', 'alice@example.com', '2023-10-25']);
xlsx.writeRow(['Bob Johnson', 'bob.j@domain.com', '2023-10-26']);
xlsx.writeRow(['Charlie Brown', 'charlie@brown.com', '2023-10-27']);
// 4. Finalize the file. This step is crucial to save the file properly.
xlsx.finalize();
console.log('Excel file created successfully!');
استریمینگ برای مجموعهدادههای بزرگ
در اینجا Node-XLSX-Writer بهخوبی میدرخشد. بهجای بارگذاری تمام دادهها در حافظه، میتوانید سطرها را یکییکی استریم کنید. این روش برای پردازش نتایج پرسوجوهای پایگاهداده مناسب است. کتابخانه از توابعی استفاده میکند تا هر رکورد را از پایگاهداده استخراج کند. بلافاصله آن را بهعنوان یک سطر جدید در فایل Excel مینویسیم. این رویکرد مصرف حافظه را ثابت نگه میدارد، چه در حال خروجیگیری ۱۰۰ سطر باشید و چه ۱ میلیون سطر.
How to Perform Streaming from a Database inside Node.js Apps?
// Import the library
const XLSXWriter = require('node-xlsx-writer');
// 1. Create a new instance, specifying the output file path.
const xlsx = new XLSXWriter('./output/simple_report.xlsx');
// 2. Write the header row (optional, but recommended).
xlsx.writeRow(['Name', 'Email', 'Sign-Up Date']);
// 3. Write some data rows.
xlsx.writeRow(['Alice Smith', 'alice@example.com', '2023-10-25']);
xlsx.writeRow(['Bob Johnson', 'bob.j@domain.com', '2023-10-26']);
xlsx.writeRow(['Charlie Brown', 'charlie@brown.com', '2023-10-27']);
// 4. Finalize the file. This step is crucial to save the file properly.
xlsx.finalize();
console.log('Excel file created successfully!');
قالببندی سفارشی (فونت، رنگ، حاشیهها)
اگرچه بهاندازهٔ مجموعههای کامل ویژگیدار نیست، کتابخانهٔ منبع باز Node-XLSX-Writer گزینههای استایلسازی اساسی را برای حرفهایکردن گزارشهای شما فراهم میکند. این کتابخانه از ویژگیهای مختلفی برای مدیریت استایلها در صفحاتگسترده پشتیبانی میکند، مانند بولد یا ایتالیک کردن فونت، تنظیم رنگ پسزمینه و رنگ حاشیه، تعریف سبک حاشیه (نازک، متوسط، ضخیم) و غیره. مثال زیر نشان میدهد که چگونه توسعهدهندگان نرمافزار میتوانند سطر سرعنوان را در برنامههای Node.js استایل دهند.
How to Apply Style to Header Row via Node.js Library?
const XLSXWriter = require('node-xlsx-writer');
const xlsx = new XLSXWriter('./output/styled_report.xlsx');
// Define a style object for the header
const headerStyle = {
bold: true,
background: 'FF2A52F5', // Blue background (ARGB Hex format)
color: 'FFFFFFFF', // White text
border: 'thin', // Thin border around cells
borderColor: 'FF000000' // Black border
};
// Apply the style when writing the header row
xlsx.writeRow(['Product', 'Category', 'Price', 'Stock'], headerStyle);
// Data rows are written without a style (or with a different one)
xlsx.writeRow(['Laptop', 'Electronics', 999.99, 15]);
xlsx.writeRow(['Desk Chair', 'Furniture', 249.50, 8]);
xlsx.finalize();