今天在使用POI导出数据时,出现如下错误:
ES查询阅读推荐比:336 resList:87555 start:89500 写入excel Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1700) at org.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1303) at org.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1190) at org.apache.xmlbeans.impl.store.Saver$TextSaver.emitElement(Saver.java:962) at org.apache.xmlbeans.impl.store.Saver.processElement(Saver.java:476) at org.apache.xmlbeans.impl.store.Saver.process(Saver.java:307) at org.apache.xmlbeans.impl.store.Saver$TextSaver.saveToString(Saver.java:1864) at org.apache.xmlbeans.impl.store.Cursor._xmlText(Cursor.java:546) at org.apache.xmlbeans.impl.store.Cursor.xmlText(Cursor.java:2436) at org.apache.xmlbeans.impl.values.XmlObjectBase.xmlText(XmlObjectBase.java:1500) at org.apache.poi.xssf.model.SharedStringsTable.getKey(SharedStringsTable.java:142) at org.apache.poi.xssf.model.SharedStringsTable.addEntry(SharedStringsTable.java:188) at org.apache.poi.xssf.usermodel.XSSFCell.setCellValue(XSSFCell.java:462) at org.apache.poi.xssf.usermodel.XSSFCell.setCellValue(XSSFCell.java:426) at com.mkit.export.util.Write2File.writeTopic2File(Write2File.java:93) at com.mkit.export.util.FindWeMedia.findWeMedia(FindWeMedia.java:110) at com.mkit.export.main.ExportWeMedia.main(ExportWeMedia.java:13)
开始我以为是java分配内存不够用,使用命令指定JVM大小后仍然出现错误。
/usr/local/java/jdk1.8.0_121/bin/java -Xmx1024m -Xms512m -jar exWeMedia.jar
最后发现是EXCEL问题:
Excel2003的最大行是65536行,从Excel2007开始最大行是1048576。
解决办法:
Workbook wb = new SXSSFWorkbook(); (将原来的XSSFWorkbook 改为:SXSSFWorkbook)
Sheet sheet = wb.createSheet();
如果行数大于了最大值(1048576),那么可以考虑将数据切分成多个sheet来解决问题