axios下载Excel

在vue文件中

import axios from "axios";

methods: {
    
    
	// 获取excel文件流
	getExcel(){
    
    
		axios({
    
    
			headers: {
    
    
				"Content-disposition": "attachment; filename=rzjl.xls",
				"Content-Type": "application / msexcel",
			},
			responseType: "blob"
			method: "get",
			url: '请求地址',
			params: {
    
    
				// 请求参数
				'key':'value'
			},
		}).then(res => {
    
    
			if (res && res.data != null) {
    
    
				let data = res.data;
				
				this.download(data, Date.parse(new Date()))
			}
		});
	},
	// 下载Excel
	// data:文件流
	// name:excel名称
	download(data, name) {
    
    
		if (!data) {
    
    
			return
		}
		var blob = new Blob([data], {
    
     type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8' })
		var url = window.URL.createObjectURL(blob);
		var aLink = document.createElement("a");
		aLink.style.display = "none";
		aLink.href = url;
		aLink.setAttribute("download", name + ".xls");
		document.body.appendChild(aLink);
		aLink.click();
		document.body.removeChild(aLink); //下载完成移除元素
		window.URL.revokeObjectURL(url); //释放掉blob对象
	},
}

猜你喜欢

转载自blog.csdn.net/qq_41950190/article/details/107060020