工具方法

半城伤御伤魂 提交于 2019-11-27 09:58:46

 

将table导出为xlsx

import FileSaver from 'file-saver'
import XLSX from 'xlsx'

        // /* generate workbook object from table */
        // // 转换成excel时,使用原始的格式
        let xlsxParam = {raw: true};
        // el-table的fixed属性来让某一列固定,但elementui的实现方式是:创建了两个tabledom,通过一个隐藏一个显示来实现交互效果。
        // 当导出整个el-table 就会将两个div内的table都导出,导致数据重复。移除重复table
        let fix = document.querySelector('.el-table__fixed');
        let wb;
        if (fix) {
          wb = XLSX.utils.table_to_book(document.querySelector('.el-table').removeChild(fix), xlsxParam);
          document.querySelector('.el-table').appendChild(fix);
        } else {
          wb = XLSX.utils.table_to_book(document.querySelector('.el-table'), xlsxParam);
        }

        /* get binary string as output */
        let wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST: true, type: 'array'});
        try {
          FileSaver.saveAs(new Blob([wbout], {type: 'application/octet-stream'}), '差评查询-' + (moment(new Date()).format('YYYYMMDD')) + '.xlsx');
        } catch (e) {
          if (typeof console !== 'undefined') {
            console.log(e, wbout)
          }
        } finally {
          this.isExport = false;
          this.pageSize = pageSize
        }
        return wbout;

数组对象去重

// 数组对象去重
        let hash = {};
        this.targetList = this.targetList.reduce(function (item, next) {
          hash[next.name] ? '' : hash[next.name] = true && item.push(next);
          return item
        }, [])

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!