There are a lot of modules that can do it. But if you want to control the formatting of xlsx file, then I suggest you use this below code. Rows contain data in the form of JSON array.
var excel = require('node-excel-export');
var 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'
}
}
}
};
var specification = {
"Col1": {
"displayName": 'Col1Name',
"headerStyle": styles.headerDark,
"width": 250
},
"Col2": {
"displayName": 'Col2Name',
"headerStyle": styles.headerDark,
"width": 215
},
"Col3": {
displayName: 'Col3Name',
headerStyle: styles.headerDark,
width: 150
}
}
var report = excel.buildExport(
[{
name: 'Report.xlsx',
specification: specification,
data: rows
}]
);
This can then be sent either as response in the API :
res.setHeader('Content-disposition', 'attachment; filename=Report.xlsx');
res.send(report);
Or as an attachment in email:
var smtpTransport = require('nodemailer-smtp-transport');
var transporter = nodemailer.createTransport(smtpTransport({
host: "mail.yourmailman.com"
}));
transporter.sendMail({
from: "abc.gmail.com",
to: toAddr,
subject: 'scheduled reporting',
attachments: [{
'filename': 'Report.xlsx',
'content': new Buffer(report, 'base64')
}]
}, function(error, success) {
if (error) {
logger.error('Error wile sending mail ', error);
return;
} else {
logger.info('Mail sent.. ', success);
return;
}
});