Node.JS Excel-Export
Nice little module that is assisting when creating excel exports from datasets. It takes normal array-of-objects dataset plus a json report specification and builds excel(.xlsx) file. It supports styling and re-formating of the data on the fly. Check the example usage for more information.
Installation
npm install node-excel-export
Usage
- Check here, for more styling
const excel = require('node-excel-export');
const styles = {
headerDark: {
fill: {
fgColor: {
rgb: 'FF000000'
}
},
font: {
color: {
rgb: 'FFFFFFFF'
},
sz: 14,
bold: true,
underline: true
}
},
cellPink: {
fill: {
fgColor: {
rgb: 'FFFFCCFF'
}
}
},
cellGreen: {
fill: {
fgColor: {
rgb: 'FF00FF00'
}
}
}
};
const heading = [
[{value: 'a1', style: styles.headerDark}, {value: 'b1', style: styles.headerDark}, {value: 'c1', style: styles.headerDark}],
['a2', 'b2', 'c2']
];
const specification = {
customer_name: {
displayName: 'Customer',
headerStyle: styles.headerDark,
cellStyle: function(value, row) {
return (row.status_id == 1) ? styles.cellGreen : {fill: {fgColor: {rgb: 'FFFF0000'}}};
},
width: 120
},
status_id: {
displayName: 'Status',
headerStyle: styles.headerDark,
cellFormat: function(value, row) {
return (value == 1) ? 'Active' : 'Inactive';
},
width: '10'
},
note: {
displayName: 'Description',
headerStyle: styles.headerDark,
cellStyle: styles.cellPink,
width: 220
}
}
const dataset = [
{customer_name: 'IBM', status_id: 1, note: 'some note', misc: 'not shown'},
{customer_name: 'HP', status_id: 0, note: 'some note'},
{customer_name: 'MS', status_id: 0, note: 'some note', misc: 'not shown'}
]
const merges = [
{ start: { row: 1, column: 1 }, end: { row: 1, column: 10 } },
{ start: { row: 2, column: 1 }, end: { row: 2, column: 5 } },
{ start: { row: 2, column: 6 }, end: { row: 2, column: 10 } }
]
const report = excel.buildExport(
[
{
name: 'Report',
heading: heading,
merges: merges,
specification: specification,
data: dataset
}
]
);
res.attachment('report.xlsx');
return res.send(report);
Contributors
Contributor | Contribution |
---|
@jbogatay | Allow null values |
@frenchbread | Example update |
@fhemberger | Undefined header style |
@zeg-io Tony Archer | Cell Merging |
@martin-podlubny | Number and Dates custom formatting |