尝试使用以下代码编写excel文件
public static void main(String[] args) { XSSFWorkbook workbook = new XSSFWorkbook(); XSSFSheet sheet = workbook.createSheet("Book Data"); Mapdata = new TreeMap (); data.put("1", new Object[] {"ID", "NAME", "LASTNAME"}); for(int i=2;i<100000;i++) { data.put(String.valueOf(i), new Object[] {i, "Name"+i, "LastName"+i}); } Set keyset = data.keySet(); int rownum = 0; for (String key : keyset) { Row row = sheet.createRow(rownum++); Object [] objArr = data.get(key); int cellnum = 0; for (Object obj : objArr) { Cell cell = row.createCell(cellnum++); if(obj instanceof String) cell.setCellValue((String)obj); else if(obj instanceof Integer) cell.setCellValue((Integer)obj); } } try { FileOutputStream out = new FileOutputStream(new File("D:\\nameandlname.xlsx")); workbook.write(out); out.close(); System.out.println("nameandlname.xlsx written successfully."); } catch (Exception e) { e.printStackTrace(); } }
VM arguments -Xms512M -Xmx1024M
的eclipse.ini:
-startup plugins/org.eclipse.equinox.launcher_1.1.0.v20100507.jar --launcher.library plugins/org.eclipse.equinox.launcher.win32.win32.x86_1.1.1.R36x_v20100810 -product org.eclipse.epp.package.jee.product --launcher.defaultAction openFile --launcher.XXMaxPermSize 1024M -showsplash org.eclipse.platform --launcher.XXMaxPermSize 256m --launcher.defaultAction openFile -vmargs -Dosgi.requiredJavaVersion=1.5 -Xms512m -Xmx1024m -vm C:/Program Files (x86)/Java/jdk1.6.0_21/bin/javaw.exe
最后得到错误:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at org.apache.xmlbeans.impl.store.Saver$TextSaver.resize(Saver.java:1592) at org.apache.xmlbeans.impl.store.Saver$TextSaver.preEmit(Saver.java:1223) at org.apache.xmlbeans.impl.store.Saver$TextSaver.emit(Saver.java:1144)
vadchen.. 6
您需要的是SXSSF(Streaming Usermodel API).
SXSSF(包:org.apache.poi.xssf.streaming)是XSSF的API兼容流式扩展,用于在必须生成非常大的电子表格时使用,并且堆空间有限.
还有就是页面上的例子太多:你需要替换XSSFWorkbook
用SXSSFWorkbook
.
您需要的是SXSSF(Streaming Usermodel API).
SXSSF(包:org.apache.poi.xssf.streaming)是XSSF的API兼容流式扩展,用于在必须生成非常大的电子表格时使用,并且堆空间有限.
还有就是页面上的例子太多:你需要替换XSSFWorkbook
用SXSSFWorkbook
.