如题

我在后台封装下载excel的接口逻辑时,文件流关闭后响应返回success,但是后台报错

原因就是我系统整体封装的响应逻辑,所以我在下载文件后也返回了成功和失败的逻辑。但是并不支持。

try {

 List<ExportExcelData> excelDataList = getData(wo_id);  // 获取表体数据
 WriteCellStyle headWriteCellStyle = new WriteCellStyle();
 //设置头居中
 headWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
//内容策略
WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
//设置 水平居中
contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
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(work_order+"报表导出测试", "UTF-8").replaceAll("\\+", "%20");
//            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
response.addHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
response.addHeader("Access-Control-Expose-Headers", "Content-disposition");
// 合并策略
Map<String, List<RowRangeDto>> srategyMap = ExcelUtil.addMergeStrategy(excelDataList);
 // 这里需要设置不关闭流;
EasyExcel.write(response.getOutputStream(), ExportExcelData.class).autoCloseStream(Boolean.FALSE)
                    // 注册合并策略
                    .registerWriteHandler(new BizMergeStrategy(srategyMap))
                    .registerWriteHandler(ExcelUtil.CellStyleStrategy())
                    .sheet("ckd出货信息表")
                    .doWrite(excelDataList);
 return "文件下载成功";    // 改为void,不反回               
} catch (Exception e) {
            // 重置response
response.reset();
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
logger.error(e.getMessage());
 e.printStackTrace();
 return "文件下载失败";   // 改为void,不反回
        }

一个解决办法是并不返回提示,或者返回null即可避免此类报错。

但是很遗憾,如果我非要想返回 成功或失败提示,目前还没有办法。

更多推荐

解决No converter for XXX with preset Content-Type ‘application/octet-stream;charse