后端返回base64格式数据转excel格式文件并下载
效果

组件
function dataURLtoBlob(base64result,filename) {
var raw = window.atob(base64result);
var uInt8Array = new Uint8Array(base64result.length);
for (var i = 0; i < raw.length; i++) {
uInt8Array[i] = raw.charCodeAt(i);
}
const link = document.createElement("a");
const blob = new Blob([uInt8Array],{
type: 'application/vnd.ms-excel'
})
link.style.display = 'none';
link.href = URL.createObjectURL(blob);
link.setAttribute('download', filename +'.xls');
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
export {
dataURLtoBlob
}
使用
import {
dataURLtoBlob } from '../../../utils/index'
async hDownloadData() {
const res = await getexcelData()
let base64Data = res.data.base64Data
let fileName = res.data.fileName
dataURLtoBlob(base64Data, fileName)
}