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