vue 导出excel兼容IE用blob的形式

//导出excel
Vue.prototype.exportExcel = function(url, name, searchform) {//我在main.js定义了一个全局的函数
	//url 接口地址
	//name 导出的文件名
	//searchform 导出时接口的参数信息
	let exportExcelUrl = "";
	if (process.env.NODE_ENV === 'development') {
		exportExcelUrl = '/export/' + url;
	} else {
		exportExcelUrl = '/scm/export/' + url;
	};
	this.$axios({
		method: 'get',
		url: exportExcelUrl,
		params: searchform,
		responseType: 'blob'
	}).then(response => {
		if (!response) {
			return
		}
		const blob = new Blob([response.data], {
			type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'
		})
		
		
		
		if (!!window.ActiveXObject || "ActiveXObject" in window) {//判断是不是ie的浏览器
			window.navigator.msSaveOrOpenBlob(blob,name + '.xls');
		}else{
    		const downloadElement = document.createElement('a')
    		const href = window.URL.createObjectURL(blob)
    		downloadElement.href = href
    		downloadElement.download = name + '.xls'
			document.body.appendChild(downloadElement)
			downloadElement.click()
			document.body.removeChild(downloadElement) // 下载完成移除元素
			window.URL.revokeObjectURL(href) // 释放掉blob对象
		}
		
		
		
	}).catch((error) => {

	})
}

你可能感兴趣的:(VUE)