一、直接上代码(部分代码)
<el-button
type="success"
icon="el-icon-download"
@click="batchExport"
>批量导出</el-button>
batchExport() {
const params = {
orderType: this.queryInfo.type,
userName: this.queryInfo.user,
orderNum: this.queryInfo.number
}
if (this.queryInfo.date) {
params.startTime = this.queryInfo.date[0]
params.endTime = this.queryInfo.date[1]
} else {
params.startTime = `${this.getCurrentDate()} 00:00:00`
params.endTime = `${this.getCurrentDate()} 23:59:59`
}
getFlow(params).then(res => {
this.downloadFile(res, '订单列表.xls')
})
},
downloadFile(data, filename) {
if (typeof window.chrome !== 'undefined') {
const link = document.createElement('a')
link.href = window.URL.createObjectURL(data)
link.download = filename
link.click()
} else if (typeof window.navigator.msSaveBlob !== 'undefined') {
const blob = new Blob([data], { type: 'application/force-download' })
window.navigator.msSaveBlob(blob, filename)
} else {
const file = new File([data], filename, { type: 'application/force-download' })
window.open(URL.createObjectURL(file))
}
},
getCurrentDate() {
const dt = new Date()
const year = dt.getFullYear()
const month = String(dt.getMonth() + 1).padStart(2, '0')
const date = String(dt.getDate()).padStart(2, '0')
return `${year}-${month}-${date}`
}
const getFlow = params => {
return axios.get2(baseUrl + '/TrainingCenter/order/export', { params })
}
get2(url, data) {
const options = Object.assign(
{
method: 'get',
url,
responseType: 'blob'
},
data
)
return this.request(options)
}