Vue导出Excel乱码问题(已解决)

后端已经写好了导出Excel的接口,调用接口会发现后端返回的是二进制流文件

实现方法:

在封装接口时,给接口传递一个参数 【 responseType: 'blob' 】   该参数是处理导出Excel表格数据乱码问题的关键

// 导出订单
export const exportOrder = query => {return request({url: '/shop/order.php',method: 'post',params: query,responseType: 'blob'});};

 在页面中写实现方法

// 订单导出
getOrderExport(){
	exportOrder(this.query).then(res => {
		console.log(res);
		const link = document.createElement('a');// 创建a标签
		let blob = new Blob([res],{type: 'application/vnd.ms-excel;charset=UTF-8'}); // 设置文件类型 
		link.style.display = "none";
		link.href = URL.createObjectURL(blob); // 创建URL
		link.setAttribute("download", "订单.xls");
		document.body.appendChild(link);
		link.click();
		document.body.removeChild(link);
	})
},

猜你喜欢

转载自blog.csdn.net/weixin_45990765/article/details/122249441