前后端分离利用Blob实现导出excel

传统jsp项目中实现excel的导出

利用后端服务返回输出流,添加header,设置content-type
```
response.addHeader("Content-Disposition", "attachment;filename="+fileName)
response.setContentType("application/ynd.ms-excel;charset=UTF-8");

## 前后端分离导出
 js对象Blob能存储大量的二进制数据,利用这个特性将后端服务返回的流存储到blob
 ```
import axios from 'axios'
export function exportExcel(url,param,fileName) {
axios({
 method: "GET",
 url: url,
 params: param || {},
 responseType: 'blob'
}).then(r => {
 let blob = new Blob([r.data], {type: 'application/vnd.ms-excel'});
 window.URL = window.URL || window.webkitURL;
 let href = URL.createObjectURL(blob);
 let downA = document.createElement("a");
 downA.href =  href;//
 downA.download = fileName;
 downA.click();
 window.URL.revokeObjectURL(href);
})
}
 ```

你可能感兴趣的:(前后端分离利用Blob实现导出excel)