思路:通过两次请求来解决,数据量如果巨大请绕过
在此之前需导入
import XLSX from 'xlsx'
import FileSaver from 'file-saver'
HTML片段
<el-button @click="exportExcel">导出</el-button>
//这里加上个id
<el-table id="table" :data="table" >
// 导出页面全部数据
async exClick() {
// 当前分页页数
const oldPageNum = this.form.pageNum
// 当前分页条数
const oldPageSize = this.form.pageSize
// 导出数据 请求一页
this.form.pageNum = 1
// 请求所有条数
this.form.pageSize = this.form.total
axios.post('/xxxx/xxxxx', qs.stringify(this.form)).then(res => {
this.table = res.list
if (res.list.length > 5000) {
return this.$message.error('数据量大于5000,请联系管理员导出!')
}
this.$nextTick(() => {
this.exportExcel(oldPageNum, oldPageSize)
})
})
},
exportExcel(page, size) {
const xlsxParam = { raw: true }
const wb = XLSX.utils.table_to_book(document.querySelector('#table'), xlsxParam)
const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
console.log(wb.Sheets.Sheet1)
try {
FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), 'xxxx.xlsx')
} catch (e) {
if (typeof console !== 'undefined') console.log(e, wbout)
}
// 导出之前的页数
this.form.pageNum = page
// 导出之前的条数
this.form.pageSize = size
//再次请求
this.getData()
return wbout
}