问题描述
提示:这里描述项目中遇到的问题:
前端使用vue 后端使用easyexcel 导出excl文件,出现下图内容
后端代码
@Override
public void selectListExport(HttpServletResponse response, ProductOrderPage page) throws IOException {
PageHelper.startPage(page.getPage(), 10000000, true);
List<ProductOrderExcelVo> dataList = new ArrayList<>();
List<ProductOrderVo> daRlt = mapper.selectListPage(page);
daRlt.forEach(it ->{
ProductOrderExcelVo p = new ProductOrderExcelVo();
BeanUtils.copyProperties(it, p);
dataList.add(p);
} );
WriteCellStyle headWriteCellStyle = new WriteCellStyle();
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
HorizontalCellStyleStrategy horizontalCellStyleStrategy = new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("测试", "UTF-8").replaceAll("\\+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), ProductOrderExcelVo.class).sheet("模板").doWrite(dataList);
}
原因分析:
提示:这里填写问题的分析:
返回数据res 是一个object对象 需要使用 res.data 拿到 数据流
解决方案:
提示:这里填写该问题的具体解决方案:
axios({
method: 'post',
url:PramsUrl,
data:preOrderNoIds,
responseType:"arraybuffer"
}).then((res)=>{
let url = window.URL.createObjectURL(new Blob([res.data], { type: '.xlsx' }));
let a= document.createElement('a');
a.style.display = 'none';
a.href = url;
a.setAttribute('download', `${this.downloadName}.xlsx`);
document.body.appendChild(a);
a.click();
url = window.URL.revokeObjectURL(url);
document.body.removeChild(a)
}).catch(error => {
this.$message.error('导出失败')
});