node 报表导出地址

https://github.com/zeMirco/json2csv

http://www.cnblogs.com/cjingzm/p/3196424.html

 

//第一步引入模块

var json2csv = require('json2csv');
var fs = require('fs');
var Csv = require('csv');
var crypto = require('crypto');
var iconv = require('iconv-lite');

 

//第二步

{
method: ['get'],
path: '/export/{excelname}',
config: {
tags: ['api'],
description: '将物业托管信息导出到excel',
notes: '好好',
validate: {
params: joi.object({
excelname: joi.string().description('批次名称'),
}),
failAction: FailAction
}
},
handler: (request, reply) => {
var cookie = request.state;
const excelname = request.params.excelname;
if(cookie.ymciadmin) {
let accountobj = JSON.parse(security.aes.decrypt(cookie.ymciadmin));
if(accountobj.type!=0||accountobj.state==0)
{
return reply({
code:563,
msg:"你没有导出的权限!",
data:{}

})
}
var filter = {};
Estate.readAll(filter, (err, result) => {
if(result.length == 0) {
return reply({
code: 530,
msg: '物业托管表无数据!',
data: null
});
} else {
let data=[];
data[0]={"_id":"序号","name":"姓名","IDCard":"身份证","mobile":"电话号码","estateproject":"project","estatenumber":"编号"}
for(var i=0;i<result.length;i++)
{
data[i+1]=result[i];
}
Csv.stringify(data, {
columns: ['_id','name','IDCard','mobile','estateproject','estatenumber']
}, (err, output) => {
if(err) {
return reply({
code: 400,
msg: "下载出错!!!",
data: null
});
}
output = output.replace(/\"/g, "")
// return reply(output)
// const buffer = new Buffer(output, 'utf8');
// const str = iconv.encode(buffer, 'gb2312');
reply(output.toString('binary')).type('text/csv').header('Content-Disposition', `attachment;filename=${excelname}.csv`);
});

}
})
} else {
reply({
code: 510,
msg: '登录超时,请重新登录!',
data: null
});
}

}

},

转载于:https://www.cnblogs.com/luoliangfei/p/7339031.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值