EasyExcel快速上手
背景說明
EasyExcel是一個基于Java的簡單誉券、省內(nèi)存的讀寫Excel的開源項目。在盡可能節(jié)約內(nèi)存的情況下支持讀寫百M的Excel兑牡。
解決方案
開源項目
https://github.com/alibaba/easyexcel
官方文檔
https://www.yuque.com/easyexcel
https://alibaba-easyexcel.github.io/
依賴引入
https://mvnrepository.com/artifact/com.alibaba/easyexcel
常見問題
服務(wù)器環(huán)境或Docker容器運行失敗
問題說明
本地調(diào)試功能正常厕吉,部署至服務(wù)器或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
解決方案
服務(wù)器環(huán)境執(zhí)行如下命令
$ yum install dejavu-sans-fonts fontconfig -y
容器環(huán)境在dockerfile
文件中添加
RUN yum install dejavu-sans-fonts fontconfig -y
項目啟動參數(shù)追加
-Djava.awt.headless=true