zoukankan      html  css  js  c++  java
  • node 把数据下载成excel表格

    //依赖 excel-export

    var nodeExcel = require('excel-export');
    
    module.exports=async function (request, response) {
        var list=[{"name":"张三","age":18},{"name":"李四","age":15}]//定义测试数据
        var heads=["姓名,"年龄"]
        var fields=['name','age']
    
        let data=await downExcel(list,fields,heads)
    
        response.setHeader('Content-Type', 'application/vnd.openxmlformats');
        var fileName = "学生列表.xlsx";  //设置文件名称
        response.setHeader("Content-Disposition", "attachment; filename=" + new Buffer(fileName).toString('binary'));
        response.end(data, 'binary');
    }  
    
    //封装下载对象
    async function  downExcel(list,fields,heads) {
            var datas = new Array();
            for (var i = 0; i < list.length; i++) {
                var parms = [];
                for (var j = 0; j < fields.length; j++) {
                    if (list[i][fields[j]] === null) {
                        parms.push("");
                    } else {
                        parms.push(list[i][fields[j]] + "");
                    }
                }
                datas.push(parms); //表数据
            }
            var conf = {};
            conf.cols = []
            //设置表头字段
            for (var i in heads) {
                let head=  {caption: heads[i],type: 'string'}  
                conf.cols.push(head)
            }
            conf.rows = datas;
            var result = nodeExcel.execute(conf);
            return   result
        }
    }
  • 相关阅读:
    美工代码注意事项(html+div+css+js)
    vss搭建于操作
    那些年用过的好工具
    javascript 和jqurry详解
    jeecg list页面鼠标悬停展示内容
    页面自适应优化备份
    查询数据字典备份
    页面 微操作备份
    小数保留位数
    从阿里云那文件 解析xml备份
  • 原文地址:https://www.cnblogs.com/ashion89/p/12718043.html
Copyright © 2011-2022 走看看