<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>base64ToExcel</title>
</head>
<body>
请输入base64串
<button onclick="submit()">生成excel</button>
<br/><br/>
<input id="test" style="width:800px;height:30px;" />
<script>
function submit(){
let data = document.getElementById('test').value
saveData(data, 'test.xlsx')
}
function saveData(data, filename) {
let blob = dataURItoBlob(data)
let objectUrl = URL.createObjectURL(blob);
//let spl = filename.split(".");
//window.open(objectUrl,'_blank','alwaysRaised=yes,height=500, width=800, toolbar= no, menubar=no, scrollbars=no, resizable=no, location=yes, status=no,top=100,left=300')
let aLink = document.createElement('a');//新添加的 尝试
let evt = document.createEvent("HTMLEvents");
evt.initEvent("click",true,true);
aLink.download = filename;//新添加的 尝试
aLink.href=objectUrl;
aLink.click()
//没有清理的 DOM 元素引用
//document.body.removeChild(aLink)
}
/**
* base64 to blob二进制
*/
function dataURItoBlob(dataURI) {
var byteString = atob(dataURI.split(',')[0]); //base64 解码
var arrayBuffer = new ArrayBuffer(byteString.length); //创建缓冲数组
var intArray = new Uint8Array(arrayBuffer); //创建视图
for (var i = 0; i < byteString.length; i++) {
intArray[i] = byteString.charCodeAt(i);
}
// return new Blob([intArray],{type: "application/vnd.ms-excel;charset=utf-8;"});//原来封装好的 下载的是excel
return new Blob([intArray],{type: "application/octet-stream;charset=utf-8;"});//修改-1
}
</script>
</body>
</html>
网友评论