开发环境:golang vue
第一步;安装2个插件,运行npm install xlsx npm install file-saver
第二步:项目中引进插件(组件中引入)
<script>
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
</script>
第三部:方法中定义事件处理函数,#out-table为绑定table的id,run_rank.xlsx为导出的excel的文件名称
//导出到excel
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' }), 'run_rank.xlsx')
} catch (e) { if (typeof console !== 'undefined') console.log(e, wbout) }
return wbout
},
第四部:html中设置触发按钮,绑定table对象
<!--查询按钮-->
<el-button type="success" style=" margin-right: 3%; float: right;" @click="exportExcel">导出到Excel</el-button>
。。。。。。。
<el-table id="out-table" :data="print_data" style="width: 97%" @current-change="handleCurrentChange">
搞定