码迷,mamicode.com
首页 > 其他好文 > 详细

POI 生成excel(大数据量) SXSSF

时间:2019-01-04 12:17:24      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:tno   exce   can   instance   port   cannot   creates   write   oid   

使用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

标签:tno   exce   can   instance   port   cannot   creates   write   oid   

原文地址:https://www.cnblogs.com/forward/p/10218471.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!