1、安装依赖

安装了俩依赖,xlsx和file-saver

npm install --save xlsx file-saver

参考链接:

https://github.com/SheetJS/js-xlsx 
https://github.com/eligrey/FileSaver.js

2、页面引入

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

3、methods中引入方法

其中参数id是是el-table的id,title是导出的excel的名字

exportExcel (id,title) {
  /* generate workbook object from table */
  var wb = XLSX.utils.table_to_book(document.querySelector('#'+id))
  /* 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' }), title+'.xlsx')
   } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
   return wbout
}

4、使用

<el-button type="primary" @click="exportExcel('studentTable','学生信息')">表格导出</el-button>

5,此时这样做是可以导出表格的,但有个问题是导出的数据是重复的,详细原因及解决方法http://inmwang.com/#/detail/5

有一个需要注意的点,判断是否存在重复的表格时,因为每个人的设置不同,判断的依据(也就是某个class)不同

我的是el-table__fixed-right,而解决方案里的是el-table__fixed,到时候注意这个即可

// 判断要导出的节点中是否有fixed的表格,如果有,转换excel时先将该dom移除,然后append回去,
var fix = document.querySelector('.el-table__fixed-right')

至于是哪个class,如图

Logo

前往低代码交流专区

更多推荐