const res = await exportManualList({
fields: exportCodes,
ids: this.allCheckList
})
const href = window.URL.createObjectURL(res)
const link = document.createElement('a')
link.href = href
link.download = '切片清单' + moment().format('YYYY-MM-DD HH:mm:ss') + '.xlsx'
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
window.URL.revokeObjectURL(href)
接口:
export const exportManualList = (data) => {
return request({
url: 'aaaaaaaaaaa',
method: 'post',
data,
responseType: 'blob'
})
}
这里一定要 responseType: 'blob',要处理后端给的文件流
以上内容直接复制粘贴就可以使用,有问题可以评论问哈。
第二种前端导出方式:导出table表格内容,首先给table表格设置id,id="outTable",
下面是导出方法
import XLSX from 'xlsx'
const FileSaver = require('file-saver')
exportSelections() {
if (this.selectedTableData.length === 0) {
this.$message.error('请选择需要导出的数据')
return
}
const time = new Date().getTime()
const random = this.getRandomNum(100, 1000)
const wb = XLSX.utils.table_to_book(document.querySelector('#outTable'), { raw: true })
const wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: true, type: 'array' })
FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), `病例数据${time}-${random}.xlsx`)
},
// 生成随机数
export function getRandomNum(Min, Max) {
var Range = Max - Min
var Rand = Math.random()
return (Min + Math.round(Rand * Range))
}