无需请求等待,秒下载excel表格

安装依赖

npm install --save xlsx file-saver

引入依赖

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

方法

exportExcel(excelName) {
      try {
        const $e = this.$refs['report-table'].$el
        let $table = $e.querySelector('.el-table__fixed')
        if(!$table) {
          $table = $e
        }

        const wb = XLSX.utils.table_to_book($table, {raw:true})
        const wbout = XLSX.write(wb, {bookType: 'xlsx', bookSST:true, type: 'array'})
        FileSaver.saveAs(
          new Blob([wbout],{type: 'application/octet-stream'}),
          `${excelName}.xlsx`,
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.error(e)
      }
    }

或者

exportExcel () {
    /* generate workbook object from table */
    var xlsxParam = { raw: true } // 导出的内容只做解析,不进行格式转换
    var table = document.querySelector('#exportTab').cloneNode(true)
    table.removeChild(table.querySelector('.el-table__fixed')) //这里是双下划线
    var wb = XLSX.utils.table_to_book(table, xlsxParam)
    /* get binary string as output */
    var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
    try {
        FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), 'fileName.xlsx')
    } catch (e) {
    if (typeof console !== 'undefined') {
        console.log(e, wbout)
    }
    }
    return wbout
}

你可能感兴趣的:(控件,excel)