node报表导出

 

var Csv = require('csv')

//const data=[{c:'111哈s哈',b:2},{c:3,b:4}];//假设这个是从数据库读取出来的json
const data = result;
const batchname = Batch_Name; //假设这个是从数据库获取到的批次名称
//如果使用中文会有乱码,网上解决方案 使用iconv 将utf8 转换为gbk iconv.convert(content);
// console.log(result)
Csv.stringify(data, {
: columns: ['c','b']//这儿是导出列//这儿是导出列,如果注释本行将导出所有列的值,不然只导出指定列的值
}, (err, output) => {
if(err) {
return reply({
code:400,
msg:"下载出错!!!",
data:null
});
}
output=output.replace(/\"/g,"")//这儿替换所有的双引号

//reply(output)
console.log(output)
//reply(new Buffer(output, 'utf8').toString('binary')).type('text/csv').charset('ansi').header('Content-Disposition', `attachment;filename=${batchname}.csv`);
var buffer=new Buffer(output,'utf8');
 var str=iconv.encode(buffer,'gb2312');
 reply(str.toString('binary')).type('text/csv').header('Content-Disposition', `attachment;filename=${batchname}.csv`);
});

//实例

{
method: ['get'],
path: '/vippoints/export/{excelname?}',
config: {
tags: ['api'],
description: ' 会员卡积分导出',
notes: '这里是notes对应的内容',
},
handler: (request, reply) => {
let cookie = request.state;
const excelname = request.params.excelname.trim();
if(cookie.ymciadmin) {
let accountobj = JSON.parse(security.aes.decrypt(cookie.ymciadmin));
if(accountobj.state == 0 || accountobj.type != 0) {
return reply({
code: 542,
msg: '你没有此权限'
})
}
let filter = {};
let pipeline = [{
$match: filter
},  

{
$group: {
_id: "$cardnumber",
total: {
$sum: "$point"
}
}
},
{
$sort: {
total: -1
}
}
]
VPoint.readAllsum(pipeline, (err, docs) => {
let arrs = docs;
if(docs.length != 0) {
Usercenter.readAll({}, (err, result) => {
for(var i = 0; i < result.length; i++) {
result[i].pointsum = 0;
for(var j = 0; j < arrs.length; j++) {
if(result[i].vipcardnumber == arrs[j]._id) {
result[i].pointsum = arrs[j].total;
}
}
}
// console.log(result)
let data = [];
data[0] = {
"name": "姓名",
"IDCard": "身份证",
"mobile": "电话号码",
"vipcardnumber": "会员卡号",
"vipleval": "会员级别",
"pointsum": "活动积分",
"province": "省",
"city": "市",
"area": "县/区",
"address_detail": "详情地址",

}
for(var i = 0; i < result.length; i++) {
result[i].IDCard = "\'" + result[i].IDCard //身份证长度超过15位需要在前加单引号
data[i + 1] = result[i];
}
Csv.stringify(data, {
columns: ['name', 'IDCard', 'mobile', 'vipcardnumber', 'vipleval', 'pointsum', 'province', 'city', 'area', 'address_detail']
}, (err, output) => {
if(err) {
return reply({
code: 400,
msg: "下载出错!!!",
data: null
});
}
output = output.replace(/\"/g, "")
//reply(buffer.toString('binary')).type('text/csv').header('Content-Disposition', `attachment;filename=${excelname}.csv`);
// 解决不同浏览器下载文件名称乱码以及 解决excel打开内容乱码
var userAgent = (request.headers['user-agent'] || '').toLowerCase();
if(userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) {
reply(Buffer.concat([new Buffer('\xEF\xBB\xBF', 'binary'), new Buffer(output)])).type('xlsx/text/csv').header('Content-Disposition', 'attachment; filename=' + encodeURIComponent(excelname) + '.csv');
} else if(userAgent.indexOf('firefox') >= 0) {
reply(Buffer.concat([new Buffer('\xEF\xBB\xBF', 'binary'), new Buffer(output)])).type('xlsx/text/csv').header('Content-Disposition', 'attachment; filename*="utf8\'\'' + encodeURIComponent(excelname)+'.csv"');
} else {
reply(Buffer.concat([new Buffer('\xEF\xBB\xBF', 'binary'), new Buffer(output)])).type('xlsx/text/csv').header('Content-Disposition', 'attachment; filename=' + new Buffer(excelname).toString('binary')+'.csv');
}

});

})
} else {
reply({
code: 235,
msg: '未找到相关信息!',
data: {}
});
}
})
} else {
reply({
code: 510,
msg: '登录失败,请重新登陆!',
data: {}
});
}
}

},

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值