一、使用 FileSaver.js
安装:
npm install file-saver --save
引用:
import FileSaver from 'file-saver'
api文件:
导出文件记得加上 responseType: ‘blob’
import axios from 'axios';
Export: (params) => {
return axios({
method:'get',
url: 'url',
responseType: 'blob', // 导出文件记得加上 responseType: 'blob'
params: params,
}).then(res => res.data)
},
使用:
const getExport = () =>{
let params = {
Treeval: Treeval || '',
operatioType: Type || '',
startTime: Time ? moment(Time[0]).format('yyyy-MM-DD HH:mm:ss') : '',
endTime: Time ? moment(Time[1]).format('yyyy-MM-DD HH:mm:ss') : ''
}
api.Export(params).then(res => {
exports(res)
})
}
const exports = (res) => {
let blob = new Blob([res], {
type: "application/vnd.ms-excel"
});
let obj = URL.createObjectURL(blob);
FileSaver.saveAs(obj, '表格名称');
}
二、使用open打开新的页面,导出文件后返回原页面
const getExport = () =>{
let params = {
Treeval: Treeval || '',
Type: Type || '',
startTime: Time ? moment(Time[0]).format('yyyy-MM-DD HH:mm:ss') : '',
endTime: Time ? moment(Time[1]).format('yyyy-MM-DD HH:mm:ss') : ''
}
var str = '';
for (var i in params) {
str += '&' + i + '=' + params[i];
}
str = str.substring(1, str.length)
window.open('url' + str)
}