如何在node js中生成excel表格直接打开或保存在系统中,而不是保存在项目目录中。
For Excel
exports.excel = function (req, res) {
var fs =require('fs');
var excel = require('excel4node');
var workbook = new excel.Workbook();
var worksheet = workbook.addWorksheet('Sheet 1');
var worksheet2 = workbook.addWorksheet('Sheet 2');
worksheet.cell(1, 1).string('content for display');
workbook.write('report.xlsx');
workbook.co(function(ok){
if (!ok)
workbook.cancel();
else
console.log('congratulations, your workbook created');
});
//res.download();
//workbook.close();
//res.redirect('/invoice/list');
};
最佳答案
如果您想流式传输 report.xlsx
的内容到网络浏览器,那么您需要在 console.log('congratulations, your workbook created')
之后添加以下行:
var filePath = path.join(__dirname, 'report.xlsx');
var stat = fs.statSync(filePath);
var ct = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
res.writeHead(200, {
'Content-Type': ct,
'Content-Length': stat.size
});
var readStream = fs.createReadStream(filePath);
readStream.pipe(res);
关于javascript - 生成 PDF 和 Excel 工作表,但在 Excel 中它既不打开也不选择保存....(就像在 Laravel 中一样),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50780826/