Excel导出碰到的一些问题

1. 空指针异常

java.lang.NullPointerException: null
        at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
        at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
        at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
        at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
        at sun.font.SunFontManager$2.run(SunFontManager.java:431)

相关issue: https://github.com/docker-library/openjdk/issues/73`

根据提示得出使用的jdk缺少字体, 所以在生成图表时获取不到font导致NPE

修改Dockerfile:

#FROM openjdk:8-jdk-alpine

# openjdk:8-jdk-alpine with font support
# https://hub.docker.com/r/dquintela/openjdk-8-jdk-alpine/dockerfile
FROM dquintela/openjdk-8-jdk-alpine
COPY apps/app.jar app.jar
COPY run.sh run.sh
ENTRYPOINT sh run.sh && tail -f /dev/null

重新构建镜像即可

2. 导出文件名乱码

private void download(HttpServletResponse response, String fileName, List> data) throws IOException {
    response.reset();

    // 数据流传输
    response.setContentType("application/octet-stream");
    // 中文文件名编码
    response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));

    Sheet sheet = new Sheet(1);
    // 去除首行空白(默认从0开始的话, 第一行会空出给head, 但我们在data中加入了head)
    sheet.setStartRow(-1); 
    
    EasyExcelFactory.getWriter(response.getOutputStream()).write0(data, sheet).finish();
  }

你可能感兴趣的:(java,spring,docker)