zoukankan      html  css  js  c++  java
  • 前端导出对象或者 JSON 数据为 CSV 文件

    参考博客

    前端导出表格为CSV文件功能

    代码

    getExportList() {
      let jsonData = {
          trade:{
            tHeader: ["名字","性别","年龄","爱好","身份证号码"],
            filterVal: ["name","sex","nlin","aihao","sfz"],
            list: [{name: "名字啊",sex: "性别啊",aihao: "爱好啊",nlin:"年龄啊",sfz:"123456789012345678"},
                  {name: "名字哦",sex: "性别哦",nlin:"年龄哦",aihao: "爱好哦",sfz:"123456789012345678"}]
          },
          order:{
            tHeader: ["附表名字","附表性别","附表年龄","附表爱好"],
            filterVal: ["name","sex","nlin","aihao"],
            list: [{name: "附表名字啊",sex: "附表性别啊",nlin:"附表年龄啊",aihao: "附表爱好啊",},
                   {name: "附表名字哦",sex: "附表性别哦",nlin:"附表年龄哦",aihao: "附表爱好哦",}]
            }
        }    
        this.exportPathMethod(jsonData)// 调用exportPathMethod对数据进行处理导出
     },
    
    exportPathMethod(data) {
        /*
        *注:csv文件:","逗号换列,\n换行,\t防止excel将长数字变科学计算法等样式
        */
    
        //要导出的json数据
        let mainLists = data.trade   //主表
        let subLists = data.order    //副表
        
        //## 数据处理
        //一级表
        let mainTitle = mainLists.tHeader;//一级标题
        let mainTitleForKey = mainLists.filterVal;//一级过滤
        let mainList = mainLists.list;//一级数据
        let mainStr = [];
        mainStr.push(mainTitle.join("\t,")+"\n");   //标题添加上换列转成字符串并存进数组
        for(let i=0;i<mainList.length;i++){
            let temp = [];
            for(let j=0;j<mainTitleForKey.length;j++){
                temp.push(mainList[i][mainTitleForKey[j]]); //根据过滤器拿出对应的值
            }
            mainStr.push(temp.join("\t,")+"\n");    //取出来的值加上逗号换列转字符串存数组
        }
        // console.log(JSON.stringify(mainStr.join("")));//打印文本
    
        //二级表
        let subTitle = subLists.tHeader;//二级标题
        let subTitleForKey = subLists.filterVal;//二级过滤
        let subData = subLists.list;//二级数据
        let subStr = [];
        subStr.push(subTitle.join("\t,")+"\t\n");
        for(let i=0;i<subData.length;i++){
            let temp = [];
            for(let j=0;j<subTitleForKey.length;j++){
                temp.push(subData[i][subTitleForKey[j]]);
            }
            subStr.push(temp.join("\t,")+"\t\n");
        }
        //两个表数组转成字符串合并
        let merged = mainStr.join("") + '\n' + subStr.join("")
        //console.log(JSON.stringify(merged));//打印结果
        
        //## 导出操作
        // encodeURIComponent解决中文乱码
        const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(merged)
        // 通过创建a标签实现
        let link = document.createElement('a')
        link.href = uri
        // 对下载的文件命名
        link.download = '客户管理列表.csv'
        document.body.appendChild(link)
        link.click()
    },
    
  • 相关阅读:
    zuanwenzhang
    win32 打印机api
    错误码linux
    sed的一个知识点
    linux 修改时区
    赵鹏雁?、、
    Linux下模块编译错误
    Android加速度传感器实现“摇一摇”,带手机振动 .
    Android百度地图基础实现(标记+GPS) .
    Android 中 getApplicationContext()、this、getApplication()之间的区别 .
  • 原文地址:https://www.cnblogs.com/cqkjxxxx/p/15656755.html
Copyright © 2011-2022 走看看