node.js中使用node-xlsx插件生成excel数据并导出
1.安装node-xlsx
npm install node-xlsx
2.引入模块
var xlsx = require(‘node-xlsx‘); var fs = require(‘fs‘);
3.代码
router.get(‘/export.html‘,function(req,res){ billModel.find().exec(function(err,data){ var datas=[]; var title = [‘客户名称‘,‘项目名称‘,‘设备名称‘]//这是第一行 俗称列名 datas.push(title); data.forEach((element) => { var arrInner = []; arrInner.push(element.companyName); arrInner.push(element.companyName); arrInner.push(element.projectName); arrInner.push(element.ccc); datas.push(arrInner);//data中添加的要是数组,可以将对象的值分解添加进数组,例如:[‘1‘,‘name‘,‘上海‘] }); var name=‘应付账款明细‘+GetDateStr()+‘.xlsx‘; writeExcel(name,datas); res.download(‘./public/bill/‘+name); }); }); function writeExcel(name,data){ var buffer=xlsx.build([{name:‘sheet1‘,data:data}]); fs.writeFileSync(‘./public/bill/‘+name+‘.xlsx‘,buffer,{‘flag‘:‘w‘}); } function GetDateStr(){ return new Date().toLocaleDateString(); }
相关推荐
wikiwater 2020-10-27
阿斌Elements 2020-06-11
zrtlin 2020-11-09
xuebingnan 2020-11-05
heheeheh 2020-10-19
Crazyshark 2020-09-15
softwear 2020-08-21
ZGCdemo 2020-08-16
jczwilliam 2020-08-16
littleFatty 2020-08-16
idning 2020-08-03
jinxiutong 2020-07-26
lanzhusiyu 2020-07-19
Skyline 2020-07-04