[alibaba/easyexcel]本地导出excel没问题,在linux服务器导出的excel文件是损坏的

2024-05-23 734 views
0

触发场景描述 在本地导出excel是正常的,但是在linux服务器导出的excel就是损坏的。

触发Bug的代码

response.setContentType("application/vnd.ms-excel; charset=utf-8");
response.setCharacterEncoding("utf-8");
String fileName = "汇总报告-" + startDate + "~" + endDate + "数据报表";
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8") + ".xlsx");
EasyExcel.write(response.getOutputStream(), ReportDownloadAllVO.class).autoCloseStream(true).sheet("汇总报告").doWrite(dataListNew);

提示的异常或者没有达到的效果 没有任何异常,但是导出的excel是损坏的。

回答

3

确认下你的数据有没有,大概率是你的数据为空。

5

我也是这个问题

0

导出的时候导出xls 试试

`public void get (@RequestBody JSONObject jsonObject , HttpServletResponse response) { try { String fileName = (String.valueOf(System.currentTimeMillis())).concat(".xls"); //response输出文件流 response.setContentType("application/vnd.ms-excel"); response.setCharacterEncoding("utf-8");

    response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
    //写入表头,表数据
    EasyExcel.write(response.getOutputStream())
            .registerWriteHandler(new ExcelWidthStyleStrategy())
            .excelType(ExcelTypeEnum.XLS)
            .head(jsonObject.getObject("head",List.class))
            .sheet("Sheet0")
            .doWrite(jsonObject.getObject("content",List.class));
    } catch (IOException e) {
        e.printStackTrace();
        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json");
    }
}`