乐趣区

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<List<String>> 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();}
退出移动版