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: {}
});
}
}
},