NodeJs之word文件生成与解析的实现代码

2020-06-17 05:51:28易采站长站整理

let SingleRow = [data[i]['id'], data[i]['provinceZh'], data[i]['leaderZh'], data[i]['cityZh']] table.push(SingleRow)
}
docx.createTable(table, tableStyle);
var out = fs.createWriteStream('out.docx');// 文件写入
out.on('error', function (err) {
console.log(err);
});
var result = docx.generate(out);// 服务端生成word
res.writeHead(200, {
// 注意这里的type设置,导出不同文件type值不同application/vnd.openxmlformats-officedocument.wordprocessingml.document
"Content-Type": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
'Content-disposition': 'attachment; filename=out' + moment(new Date().getTime()).format('YYYYMMDDhhmmss') + '.docx'
});
docx.generate(res);// 客户端导出word

第四步:抛出接口


router.put('/download/word', function (req, res) {
console.log('exportWord-------------');
docx.on('finalize', function (written) {
console.log('Finish to create Word file.nTotal bytes created: ' + written + 'n');
});

docx.on('error', function (err) {
console.log(err);
});
let fields = {
id: '',
provinceZh: '',
leaderZh: '',
cityZh: '',
cityEn: ''
}
var table = [
[{
val: "No.",
opts: {
align: "center",
vAlign: "center",
sz: '36',
// cellColWidth: 42,
// b:true,
// sz: '48',
// shd: {
// fill: "7F7F7F",
// themeFill: "text1",
// "themeFillTint": "80"
// },
// fontFamily: "Avenir Book"
}
}, {
val: "省份",
opts: {
align: "center",
vAlign: "center",
sz: '36',
// b:true,
// color: "A00000",
// align: "right",
// shd: {
// fill: "92CDDC",
// themeFill: "text1",
// "themeFillTint": "80"
// }
}
}, {
val: "市",
opts: {
align: "center",
vAlign: "center",
sz: '36',
// cellColWidth: 42,
// b:true,
// sz: '48',
// shd: {
// fill: "92CDDC",
// themeFill: "text1",
// "themeFillTint": "80"
// }
}
}, {
val: "区/县",
opts: {
align: "center",
vAlign: "center",
sz: '36',
// cellColWidth: 42,
// b:true,
// sz: '48',
// shd: {
// fill: "92CDDC",
// themeFill: "text1",
// "themeFillTint": "80"
// }
}