我要尝试从nodejs服务导出到excel的大量数据,同时生成数据对象并将其写入excel表,我想向用户显示一些状态消息。我要去服务器提取所需的数据并在服务器本身上生成excel,因为我不需要在UI上显示该数据。
我尝试在客户端中更改代码以使用$ http get,它将数据发送回客户端,但是由于其大小我无法读取较大的缓冲区。
这是我的服务器代码:
return service.getAllForExport()
.then(dataObjects => {
const sheet = {
"!ref": `A1:I${(dataObjects.length + 1}`,
A1: { t: 's', v: 'Column One' },
B1: { t: 's', v: 'Column Two' },
C1: { t: 's', v: 'Column Three' },
D1: { t: 's', v: 'Column Four' },
};
strategies = _.orderBy(strategies, ['Column One', 'Column Two', 'Column Three', 'Column Four']);
_.each(dataObjects, (data, index) => {
sheet[`A${index + 2}`] = {t: 's', v: data.valueOne};
sheet[`B${index + 2}`] = {t: 'n', v: data.valueTwo};
sheet[`C${index + 2}`] = {t: 's', v: data.valueThree};
sheet[`D${index + 2}`] = {t: 's', v: data.valueFour};
});
const workbook = {
SheetNames: ["Data"],
Sheets: {
Data: sheet
}
};
return XLSX.write(workbook, { type: 'buffer' });
})
.then(buffer => {
const fileName = `Datas_${moment().format()}.xlsx`;
res.setHeader('Content-Disposition', `attachment; filename="${fileName}"`);
res.setHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
return res.status(200).send(new Buffer(buffer));
})
客户端代码(angularjs):
this.export = () => {
$window.location.href = '/ui/data/export';
};