listenlive.cn

是谁来自山川湖海 却囿于昼夜厨房与爱


  • 首页

  • 技术

  • 笔记

  • 杂记

  • 分享

  • 归档

  • 关于

  • 搜索
close

Koa框架实现导出Excel文件

时间: 2020-12-11   |   分类: 笔记     |   阅读: 270 字 ~1分钟

安装引入excel-export包

    cnpm install excel-export --save
    const nodeExcel = require('excel-export');

实现导出方法

/**
   * 数据导出--导出方法
   * @param resultData [{},{},] //从数据库中获取的数据格式
   */
  static async exportData(resultData,ctx){
    let conf ={};
        conf.name = "worksheet";//表格名
        let alldata = new Array();
        for(let i = 0;i<resultData.length;i++){
            let arr = new Array();
            arr.push(resultData[i].name);
            arr.push(resultData[i].phone);
            arr.push(resultData[i].age);
            alldata.push(arr);
        }
        //决定列名和类型
        conf.cols = [{
            caption:'姓名',
            type:'string'
        },{
            caption:'手机号',
            type:'string'
        },{
            caption:'年龄',
            type:'string'
        }];
        conf.rows = alldata;//填充数据
        let result = nodeExcel.execute(conf);
        //最后3行express框架是这样写
        // res.setHeader('Content-Type', 'application/vnd.openxmlformats');
        // res.setHeader("Content-Disposition", "attachment; filename=" + "Report.xlsx");
        // res.end(result, 'binary');
        ctx.set('Content-Type', 'application/vnd.openxmlformats');
        ctx.set("Content-Disposition", "attachment; filename=" + "Report.xlsx");
        ctx.body=data;
  }

喜欢这篇文章的话 打赏一下吧!

Wechat Alipay

#Koa2# #Node.js#
PHP-微信公众号实现发送模板消息
Go-strings和strconv函数的使用
  • 文章目录
  • 站点概览
一刀未剪

一刀未剪

Programmer & Architect

GitHub Instagram Weibo Email
70 日志
4 分类
69 标签
工具站
Harbor 在线文档 Quick Reference Linux 命令查询 Mock 模拟数据
友情链接
  • milu杰克
  • 安装引入excel-export包
  • 实现导出方法
京ICP备17004223号 © 2020 - 2023 listenlive.cn
Powered by - HUGO
Theme by - NexT
访客量 -
0%