https://blog.csdn.net/u010427666/article/details/79208145
vue2.0 + element UI 中 el-table 數據導出Excel
1、 安裝相關依賴
主要是兩個依賴
npm install --save xlsx file-saver
1
如果想詳細看着兩個插件使用,請移步github。
https://github.com/SheetJS/js-xlsx
https://github.com/eligrey/FileSaver.js
2、組件里頭引入
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
3、組件methods里寫一個方法
exportExcel () { /* generate workbook object from table */ var wb = XLSX.utils.table_to_book(document.querySelector('#out-table')) /* 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' }), 'sheetjs.xlsx') } catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) } return wbout }, ---------------------
注意:XLSX.uitls.table_to_book( 放入的是table 的DOM 節點 ) ,sheetjs.xlsx 即為導出表格的名字,可修改!
4、點擊導出按鈕執行 exportExcel 的方法即可 。