EasyExcel快速上手

EasyExcel快速上手

背景说明

EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。在尽可能节约内存的情况下支持读写百M的Excel。

解决方案

开源项目

https://github.com/alibaba/easyexcel

官方文档

https://www.yuque.com/easyexcel
https://alibaba-easyexcel.github.io/

依赖引入

https://mvnrepository.com/artifact/com.alibaba/easyexcel

常见问题

服务器环境或Docker容器运行失败

问题说明

本地调试功能正常,部署至服务器或Docker容器时失败,异常信息如下

java.lang.NullPointerException: null
    at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264) ~[?:1.8.0_151]
    at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219) ~[?:1.8.0_151]
    at sun.awt.FontConfiguration.init(FontConfiguration.java:107) ~[?:1.8.0_151]
    at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774) ~[?:1.8.0_151]
    at sun.font.SunFontManager$2.run(SunFontManager.java:431) ~[?:1.8.0_151]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_151]
    at sun.font.SunFontManager.(SunFontManager.java:376) ~[?:1.8.0_151]
    at sun.awt.FcFontManager.(FcFontManager.java:35) ~[?:1.8.0_151]
    at sun.awt.X11FontManager.(X11FontManager.java:57) ~[?:1.8.0_151]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[?:1.8.0_151]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[?:1.8.0_151]
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[?:1.8.0_151]
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[?:1.8.0_151]
    at java.lang.Class.newInstance(Class.java:442) ~[?:1.8.0_151]
    at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83) ~[?:1.8.0_151]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_151]
    at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74) ~[?:1.8.0_151]
    at java.awt.Font.getFont2D(Font.java:491) ~[?:1.8.0_151]
    at java.awt.Font.canDisplayUpTo(Font.java:2060) ~[?:1.8.0_151]
    at java.awt.font.TextLayout.singleFont(TextLayout.java:470) ~[?:1.8.0_151]
    at java.awt.font.TextLayout.(TextLayout.java:531) ~[?:1.8.0_151]
    at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:274) ~[poi-3.16.jar!/:3.16]
    at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.(AutoSizeColumnTracker.java:117) ~[poi-ooxml-3.16.jar!/:3.16]
    at org.apache.poi.xssf.streaming.SXSSFSheet.(SXSSFSheet.java:79) ~[poi-ooxml-3.16.jar!/:3.16]
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:652) ~[poi-ooxml-3.16.jar!/:3.16]
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:673) ~[poi-ooxml-3.16.jar!/:3.16]
    at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:89) ~[poi-ooxml-3.16.jar!/:3.16]
    at com.alibaba.excel.util.WorkBookUtil.createSheet(WorkBookUtil.java:51) ~[easyexcel-1.1.2-beta4.jar!/:?]
    at com.alibaba.excel.context.WriteContext.currentSheet(WriteContext.java:112) ~[easyexcel-1.1.2-beta4.jar!/:?]
    at com.alibaba.excel.write.ExcelBuilderImpl.addContent(ExcelBuilderImpl.java:69) ~[easyexcel-1.1.2-beta4.jar!/:?]
    at com.alibaba.excel.ExcelWriter.write1(ExcelWriter.java:106) ~[easyexcel-1.1.2-beta4.jar!/:?]
参考文档

https://github.com/alibaba/easyexcel/issues/157
https://docs.qq.com/doc/DSE1HV2pTbmpTckFQ

解决方案

服务器环境执行如下命令

$ yum install dejavu-sans-fonts fontconfig -y

容器环境在dockerfile文件中添加

RUN yum install dejavu-sans-fonts fontconfig -y

项目启动参数追加

-Djava.awt.headless=true

你可能感兴趣的:(EasyExcel快速上手)