POI 生成excel(大数据量) SXSSF

Posted 宝儿的宝藏

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POI 生成excel(大数据量) SXSSF相关的知识,希望对你有一定的参考价值。

使用POI 的SXSSF (Streaming Usermodel API)生成较大的excel,同时开启压缩

 

 1 import junit.framework.Assert;
 2 import org.apache.poi.ss.usermodel.Cell;
 3 import org.apache.poi.ss.usermodel.Row;
 4 import org.apache.poi.ss.usermodel.Sheet;
 5 import org.apache.poi.ss.usermodel.Workbook;
 6 import org.apache.poi.ss.util.CellReference;
 7 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 8 
 9     public static void main(String[] args) throws Throwable {
10         SXSSFWorkbook wb = new SXSSFWorkbook(100); // keep 100 rows in memory, exceeding rows will be flushed to disk
11         Sheet sh = wb.createSheet();
12         for(int rownum = 0; rownum < 1000; rownum++){
13             Row row = sh.createRow(rownum);
14             for(int cellnum = 0; cellnum < 10; cellnum++){
15                 Cell cell = row.createCell(cellnum);
16                 String address = new CellReference(cell).formatAsString();
17                 cell.setCellValue(address);
18             }
19 
20         }
21 
22         // Rows with rownum < 900 are flushed and not accessible
23         for(int rownum = 0; rownum < 900; rownum++){
24           Assert.assertNull(sh.getRow(rownum));
25         }
26 
27         // ther last 100 rows are still in memory
28         for(int rownum = 900; rownum < 1000; rownum++){
29             Assert.assertNotNull(sh.getRow(rownum));
30         }
31         
32         FileOutputStream out = new FileOutputStream("/temp/sxssf.xlsx");
33         wb.write(out);
34         out.close();
35 
36         // dispose of temporary files backing this workbook on disk
37         wb.dispose();
38     }

 

遇到的问题:

错误是NPE错误,类似如下,原因是缺少字体或者环境变量未设置,需要安装"ttf-dejavu"字体,具体可以参考:

https://stackoverflow.com/questions/30626136/cannot-load-font-in-jre-8

https://www.jianshu.com/p/c05b5fc71bd0

 1 java.lang.NullPointerException
 2     at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
 3     at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
 4     at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
 5     at sun.awt.X11FontManager.createFontConfiguration(X11FontManager.java:774)
 6     at sun.font.SunFontManager$2.run(SunFontManager.java:431)
 7     at java.security.AccessController.doPrivileged(Native Method)
 8     at sun.font.SunFontManager.<init>(SunFontManager.java:376)
 9     at sun.awt.FcFontManager.<init>(FcFontManager.java:35)
10     at sun.awt.X11FontManager.<init>(X11FontManager.java:57)
11     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
12     at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
13     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
14     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
15     at java.lang.Class.newInstance(Class.java:442)
16     at sun.font.FontManagerFactory$1.run(FontManagerFactory.java:83)
17     at java.security.AccessController.doPrivileged(Native Method)
18     at sun.font.FontManagerFactory.getInstance(FontManagerFactory.java:74)
19     at java.awt.Font.getFont2D(Font.java:495)
20     at java.awt.Font.canDisplayUpTo(Font.java:2080)
21     at java.awt.font.TextLayout.singleFont(TextLayout.java:470)
22     at java.awt.font.TextLayout.<init>(TextLayout.java:531)
23     at org.apache.poi.ss.util.SheetUtil.getDefaultCharWidth(SheetUtil.java:275)
24     at org.apache.poi.xssf.streaming.AutoSizeColumnTracker.<init>(AutoSizeColumnTracker.java:117)
25     at org.apache.poi.xssf.streaming.SXSSFSheet.<init>(SXSSFSheet.java:79)
26     at org.apache.poi.xssf.streaming.SXSSFWorkbook.createAndRegisterSXSSFSheet(SXSSFWorkbook.java:656)
27     at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:677)
28     at org.apache.poi.xssf.streaming.SXSSFWorkbook.createSheet(SXSSFWorkbook.java:83)

 

 

 

还有一种,easyexcel,没用过,可以参考https://github.com/alibaba/easyexcel

 

以上是关于POI 生成excel(大数据量) SXSSF的主要内容,如果未能解决你的问题,请参考以下文章

HSSFXSSF和SXSSF区别以及Excel导出优化

使用 SXSSF POI 编写大型 Excel 工作表

基于SXSSF (Streaming Usermodel API)的写文件

POI读写大数据量EXCEL

Java POI实现excel大数据量下载

poi导出大数据,报内存溢出怎么解决