二进制流生成Excel

// 调用这个方法需要注意的几个点
//    1. 一定要在get或者post请求中添加  responseType: 'blob' 属性
//    2. 一定要自定义一个 fileName变量(这个是你的文件名加变量 比方 let fileName = "我的博客写的真好.xls")  fileName变量中一定要加xls或者xlsx文件后缀
//    3. dealDownload(下面这个方法)的第一个参数是后台传给你的data, 第二个参数是你定义的文件名加后缀名的字符串

// 你在调用的时候可以直接在 
.then(function(res) {
this.dealDownload(res.data, fileName) })
// 这样就Ok了 dealDownload(res,fileName){ let $blob
= new Blob([res]); if (window.navigator.msSaveOrOpenBlob) { navigator.msSaveBlob($blob,fileName); } else { // 创建 a 标签并为其添加属性 let $a = document.createElement("a"); // 创建下载链接 $a.href = window.URL.createObjectURL($blob); $a.download = fileName; document.body.appendChild($a); // 触发点击事件执行下载 $a.click(); document.body.removeChild($a); } },

 

posted on 2019-08-01 10:55  ㅤㅤㅤㅤㅤㅤ  阅读(269)  评论(0编辑  收藏  举报

导航