Biblioteca Node.js de código abierto para exportar datos a Excel XLSX
Una biblioteca líder de código abierto Node.js Excel XLSX que permite a los desarrolladores exportar datos al formato Excel XLSX mediante una API gratuita en el entorno Node.js.
Qué es Node-Excel-Export?
La biblioteca Node-Excel-Export es una herramienta potente para los desarrolladores que desean exportar datos de aplicaciones Node.js a Excel. Los desarrolladores que buscan generar hojas de cálculo de Excel de forma programática encontrarán que esta biblioteca de código abierto, disponible en GitHub, es una excelente opción gracias a sus numerosas capacidades. La biblioteca permite a los desarrolladores establecer fuentes, colores, bordes y otros elementos de estilo para cumplir con requisitos específicos o regulaciones de marca corporativa.
Node-Excel-Export es un módulo básico pero versátil que permite a los desarrolladores crear rápidamente archivos de Excel a partir de datos JSON. Esta facilidad es crucial para los usuarios que necesitan exportar datos de forma rápida y eficaz desde sus aplicaciones a un formato estándar. La biblioteca es compatible con los formatos XLSX y XLS, lo que la hace compatible con una variedad de versiones de Excel. Incluye una serie de capacidades críticas, como el mapeo de datos JSON a XLSX, la exportación de datos a archivos de Excel, la definición de estilos de fuentes, exportaciones de varias hojas, inclusión de fórmulas de Excel, compatibilidad multiplataforma y más.
The Node-Excel-Export package is simple to use and works seamlessly with Node.js apps. It is easily deployed with NPM, and its straightforward API enables software developers to start exporting data to Excel files with little preparation. As an open source project, the library benefits from the developer community's contributions and improvements. This collaborative approach ensures that the library is up to date with the most recent features and best practices. Its numerous capabilities, including customizable styles, multi-sheet support, and speed optimization, make it an essential library for data-driven applications.
Comenzando con Node-Excel-Export
The recommended way to install Node-Excel-Export is using npm, please use the following script for a smooth installation.
Instalar Node-Excel-Export vía npm
npm install excel-exportYou can download the compiled shared library from GitHub repository and install it.
Exportar datos JSON a Excel XLSX en Node.js
The Node-Excel-Export library is a powerful yet user-friendly tool that simplifies the process of generating Excel files in Node.js applications. It simplifies the process of mapping JSON data to an Excel spreadsheet. Software developers can define schemas to specify how JSON data should be translated into Excel cells. This feature ensures that the exported data retains the desired structure and format, making the resultant spreadsheets more readable and organized. The following example shows how to define a schema with styles and specify how JSON data should map to Excel columns and generates the Excel file content based on the schema and data provided.
¿Cómo generar un archivo de Excel a partir de datos JSON dentro de aplicaciones Node.js?
const excel = require('node-excel-export');
// Define a schema for the export
const styles = {
headerDark: {
fill: {
fgColor: {
rgb: 'FF000000'
}
},
font: {
color: {
rgb: 'FFFFFFFF'
},
sz: 14,
bold: true,
underline: true
}
},
cellPink: {
fill: {
fgColor: {
rgb: 'FFFFCCFF'
}
}
}
};
const specification = {
name: {
displayName: 'Name',
headerStyle: styles.headerDark,
width: 120
},
age: {
displayName: 'Age',
headerStyle: styles.headerDark,
width: 100
},
location: {
displayName: 'Location',
headerStyle: styles.headerDark,
width: 150
}
};
const dataset = [
{ name: 'John Doe', age: 30, location: 'New York' },
{ name: 'Jane Smith', age: 28, location: 'San Francisco' }
];
const report = excel.buildExport(
[
{
name: 'Report',
specification: specification,
data: dataset
}
]
);
// Save the Excel file
require('fs').writeFileSync('report.xlsx', report);
Personalizar estilos al exportar datos a XLSX en Node.js
Presentation is crucial when sharing data, and Node-Excel-Export excels in this area by allowing extensive customization of cell styles. Users can define fonts, colors, borders, and other stylistic elements to match specific requirements or corporate branding guidelines. This flexibility ensures that the exported Excel files are not only functional but also visually appealing. The following example shows how to apply custom styles to both headers and cells, ensuring that the resulting Excel file is both functional and aesthetically pleasing.
¿Cómo aplicar estilos personalizados a los encabezados y celdas de un archivo de Excel durante la exportación de datos a XLSX en Node.js?
const styles = {
headerGreen: {
fill: {
fgColor: {
rgb: 'FF00FF00'
}
},
font: {
color: {
rgb: 'FF000000'
},
sz: 12,
bold: true
}
},
cellYellow: {
fill: {
fgColor: {
rgb: 'FFFFFF00'
}
}
}
};
const specification = {
product: {
displayName: 'Product',
headerStyle: styles.headerGreen,
cellStyle: styles.cellYellow,
width: 120
},
price: {
displayName: 'Price',
headerStyle: styles.headerGreen,
width: 100
},
stock: {
displayName: 'Stock',
headerStyle: styles.headerGreen,
width: 150
}
};
const dataset = [
{ product: 'Laptop', price: 1200, stock: 30 },
{ product: 'Phone', price: 800, stock: 50 }
];
const report = excel.buildExport(
[
{
name: 'Inventory',
specification: specification,
data: dataset
}
]
);
require('fs').writeFileSync('inventory.xlsx', report);
Definir múltiples hojas al exportar datos en Node.js
The open source Node-Excel-Export library fully supports the creation of multiple sheets within a single Excel file while exporting data to Excel XLSX files inside Node.js applications. This feature is particularly useful for large datasets or when categorizing data into separate logical groups. Users can define multiple sheets within a single Excel file, each with its own schema and data. The following example shows how developers can create an Excel file with two sheets: one for user data and another for product data. Each sheet has its own schema and dataset, demonstrating the flexibility of the library.
¿Cómo crear múltiples hojas en un archivo de Excel mientras se exportan datos en Node.js?
const sheet1Specification = {
name: { displayName: 'Name', width: 120 },
age: { displayName: 'Age', width: 100 }
};
const sheet2Specification = {
product: { displayName: 'Product', width: 120 },
price: { displayName: 'Price', width: 100 }
};
const sheet1Data = [
{ name: 'John Doe', age: 30 },
{ name: 'Jane Smith', age: 28 }
];
const sheet2Data = [
{ product: 'Laptop', price: 1200 },
{ product: 'Phone', price: 800 }
];
const report = excel.buildExport(
[
{
name: 'Users',
specification: sheet1Specification,
data: sheet1Data
},
{
name: 'Products',
specification: sheet2Specification,
data: sheet2Data
}
]
);
require('fs').writeFileSync('multiple_sheets.xlsx', report);