vue中Excel导出的方法封装:
import { Loading } from 'element-ui';
import axios from '@/utils/request';
export default {
download(url,data,fileName) {
let options = {
lock: true,
text: 'Loading...',
spinner: 'el-icon-loading',
background: 'rgba(0, 0, 0, 0.7)'
}
let loadingInstance = Loading.service(options);
axios.post(url,data,'blob').then(res => {
loadingInstance.close();
if (!res) {return}
let link = document.createElement('a');
let blob = new Blob([res], {type: 'application/vnd.ms-excel'})
link.style.display = 'none';
link.href = URL.createObjectURL(blob)
link.setAttribute('download',fileName);
document.body.appendChild(link);
link.click();
document.body.removeChild(link)
}).catch(err=>{
loadingInstance.close();
})
},
}
//下载模板的实战应用:
import downloadUtil from '@/utils/download.js';
downLoad() {
//后端接口
let url = '/oms/oms/afs/api/ps_afs_cc_week/upload_template';
let fileName = 'Warranty Reserved Stock Template.xls';
let data = {}
downloadUtil.download(url, data, fileName)
},
//实战中使用导出方法导出报表数据:
import downloadUtil from '@/utils/download.js';
exportMethod() {
let myDate = new Date()
let month = myDate.getMonth() + 1
//拼接年月日时分秒
let date =myDate.getFullYear() + '' + month + '' + myDate.getDate() + '' + myDate.getHours() + '' + myDate.getMinutes() + '' + myDate.getSeconds()
let url = '/oms/oms/partSalesOrder/downloadDataFile'
let obj = JSON.parse(JSON.stringify(this.queryForm));
obj.orderType = this.tagLable;
obj.createdDate.length > 0 ? obj.createdDateStart = obj.createdDate[0] : obj.createdDateStart = 0;
obj.createdDate.length > 0 ? obj.createdDateEnd = obj.createdDate[1] : obj.createdDateEnd = 0;
let fileName = 'Search_Order_list' + date + '.xlsx'
downloadUtil.download(url,obj,fileName)
},