體驗更優排版請移步原文:http://blog.kwin.wang/programming/vue-table-export-excel-and-print.htmlhtml
頁面中顯示的table表格,常常會要求實現導出Excel的需求,項目中恰好遇到,實現起來也比較簡單,記錄一下。vue
xlsx、
file-saver
: npm install xlsx --save
npm install file-saver --save
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
//導出Excel
exportToExcel () { let et = XLSX.utils.table_to_book(document.getElementById('table-content')); //此處傳入table的DOM節點
let etout = XLSX.write(et, { bookType: 'xlsx', bookSST: true, type: 'array' }); try { FileSaver.saveAs(new Blob([etout], { type: 'application/octet-stream' }), 'trade-publish.xlsx'); //trade-publish.xlsx 爲導出的文件名
} catch (e) { console.log(e, etout) ; } return etout; }
<el-button @click="exportToExcel">導出</el-button>
//打印頁面內容
printContent(){ let wpt = document.getElementById('table-content'); let newContent = wpt.innerHTML; let oldContent = document.body.innerHTML; document.body.innerHTML = newContent; window.print(); //打印方法
window.localtion.reload(); document.body.innerHTML = oldContent; }
方法的實現很容易理解,打印的體驗也比較好,妙!npm
End…app