博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
node文件流导出excel且显示中文文件名
阅读量:6894 次
发布时间:2019-06-27

本文共 1948 字,大约阅读时间需要 6 分钟。

参考

  • 文件流导出excel

重点:Content-Disposition 文件名是中文显示

const nodeExcel = require('node-xlsx');const urlencode = require('urlencode');function exportList(ctx, config, listData, excelName) {    let excelConfig = [];    excelConfig.push(config.map(item => {      return item.title    }))    listData.forEach(list => {      excelConfig.push(config.map(item => {        const value = list[item.name];        // 不一定要有value, 因为可能是自由组合的value        return item.format && item.format(value, list) || value;      }))    })    let buffer = nodeExcel.build([{
name: excelName, data: excelConfig}]); ctx.set('Content-Type', 'application/octet-stream'); // ctx.request.headers['user-agent'] let name = urlencode(excelName + '_' + (+new Date()) + '.xlsx', "utf-8"); ctx.set("Content-Disposition", "attachment; filename* = UTF-8''"+name); // ctx.set("Content-Disposition", "attachment; filename="+ (+new Date()) + '.xlsx'); ctx.body = buffer;}module.exports = { exportList: exportList}复制代码
// config 格式const config = [{      name: 'userid',      title: '用户ID',    },{      name: 'up_time',      title: '状态时间',      format: function(value) {        return value.replace(/\.\d+/, '');      }    },{      name: 'appl_status_byhand',      // name: 'appl_status_byhand_desc',      title: '人审状态',      format: function(value) {        return value && manualStatus[value] &&  manualStatus[value].text || '-'      }    },{      name: 'available_quota/credit_quota',      title: '剩余额度/授信额度',      format: function(value, item) {        return `¥${item.available_quota/100 || 0}/¥${item.credit_quota/100 || 0}`      }    },{      name: 'user_type_desc',      title: '名单类型',      format: function(value, item) {        if(item.user_type) {          item.user_type.forEach(user => {            userTypeText.push(user_type_define[user] || '-');          });          return userTypeText.join('|');        } else {          return '';        }      }    }]复制代码

转载地址:http://ujudl.baihongyu.com/

你可能感兴趣的文章
STL——set
查看>>
TCP/IP中MSL详解
查看>>
JavaWeb学习总结(四十九)——简单模拟Sping MVC
查看>>
tar命令的使用
查看>>
linux环境变量,cp,mv命令,more,less,cat,tail,head,的使用
查看>>
ubuntu16.04下docker修改配置文件不生效解决办法
查看>>
msyql 的半同步复制
查看>>
C语言查漏补缺——const
查看>>
Druid MiddleManager Config 设置(默认只允许2个任务)
查看>>
Linux深入篇之九:构建企业级Nginx+Keepalived集群架构
查看>>
我的友情链接
查看>>
Mac升级完python后site-packages包的模块无法导入的解决办法
查看>>
hibernate二级缓存提升性能(注解方式)
查看>>
jira7.2邮箱使用
查看>>
Lucene初探
查看>>
【python】编程语言入门经典100例--34
查看>>
static和final的意义
查看>>
main 函数的两个参数
查看>>
基于pycharm的python开发配置
查看>>
多线程技术
查看>>