本文介绍了在写入xls文件与Apache POI堆错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Apache POI来创建一个Excel [的.xls]文件。现在,一个Excel可以有65535行和放大器; 256 COLS。我想编写Java code写的xls文件与65535x256细胞。我得到一个堆错误。目前堆的conf是-Xms512m -Xmx1700m。
RAM的大小为3.5GB。

什么是出路吗?
我使用HSSF *类写xls文件。

堆栈跟踪(我已经启用了-verbose:gc选项)

  [全GC [终身:1092288K-> 1092288K(1092288K),3.3174494秒] 1583807K-> 1583807K(1583808K)[烫发:3351K-> 3351K(12288K) ] 3.3174977秒] [时间:用户= 3.30 SYS = 0.03,实际= 3.33秒]
[全GC [终身:1092288K-> 1092288K(1092288K),3.3073908秒] 1583807K-> 1583807K(1583808K)[烫发:3351K-> 3351K(12288K),3.3074374秒] [时间:用户= 3.30 SYS = 0.01,实际= 3.31秒]
[全GC [终身:1092288K-> 9438K(1092288K),0.3264828秒] 1583807K-> 9438K(1583808K)[烫发:3351K-> 3351K(12288K),0.3265362秒] [时间:用户= 0.31 SYS = 0.00,实际= 0.31秒]
异常线程main堆
 高清新世代总491520K,使用44939K [0x02990000,0x23ee0000,0x23ee0000)
  伊甸园空间436928K,用10%[0x02990000,0x05572fa8,0x1d440000)
  从太空54592K,使用0%[0x1d440000,0x1d440000,0x20990000)
  空间54592K,使用0%[0x20990000,0x20990000,0x23ee0000)
 年老代总1092288K,使用9438K [0x23ee0000,0x66990000,0x66990000)
   空间1092288K,使用0%[0x23ee0000,0x24817810,0x24817a00,0x66990000)
 压实烫发根总12288K,使用3357K [0x66990000,0x67590000,0x6a990000)
   空间12288K,用27%[0x66990000,0x66cd7778,0x66cd7800,0x67590000)
没有配置共享空间。
java.lang.OutOfMemoryError:Java堆空间
    在org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:147)
    在org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:125)
    在org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:103)
    在com.test.ExcelWriter.createWorkbook(ExcelWriter.java:119)
    在com.test.TestMe2.main(TestMe2.java:38)


解决方案

同时尝试在XMS,XMX设置为相同的值即。

  -Xms1700m -Xmx1700m

这是否对你的工作?

 公共静态无效的主要(字串[] args){
  尝试{
    FileOutputStream中FILEOUT =新的FileOutputStream(POI-TEST.XLS);
    HSSFWorkbook工作簿=新HSSFWorkbook();
    CreationHelper createHelper = workbook.getCreationHelper();
    HSSFSheet工作表= workbook.createSheet(POI工作表);    的for(int i = 0; I< 20000;我++){
      鳞次栉比= worksheet.createRow(I)
      row.createCell(0).setCellValue(createHelper.createRichTextString(行+ i)段);
    }    fileOut.flush();
    workbook.write(FILEOUT);
    fileOut.close();
  }赶上(例外五){
    e.printStackTrace();
  }
}

I am using Apache POI to create an excel[.xls] file. Now an excel can have 65535 rows & 256 cols. I am trying to write the java code to write the xls file with 65535x256 cells. I am getting a heap error. The current heap conf is -Xms512m -Xmx1700m.RAM size is 3.5gb.

What is the way out for me ?I am using HSSF* classes to write xls file.

Stack Trace is (I have enabled the -verbose:gc option)

[Full GC [Tenured: 1092288K->1092288K(1092288K), 3.3174494 secs] 1583807K->1583807K(1583808K), [Perm : 3351K->3351K(12288K)], 3.3174977 secs] [Times: user=3.30 sys=0.03, real=3.33 secs]
[Full GC [Tenured: 1092288K->1092288K(1092288K), 3.3073908 secs] 1583807K->1583807K(1583808K), [Perm : 3351K->3351K(12288K)], 3.3074374 secs] [Times: user=3.30 sys=0.01, real=3.31 secs]
[Full GC [Tenured: 1092288K->9438K(1092288K), 0.3264828 secs] 1583807K->9438K(1583808K), [Perm : 3351K->3351K(12288K)], 0.3265362 secs] [Times: user=0.31 sys=0.00, real=0.31 secs]
Exception in thread "main" Heap
 def new generation   total 491520K, used 44939K [0x02990000, 0x23ee0000, 0x23ee0000)
  eden space 436928K,  10% used [0x02990000, 0x05572fa8, 0x1d440000)
  from space 54592K,   0% used [0x1d440000, 0x1d440000, 0x20990000)
  to   space 54592K,   0% used [0x20990000, 0x20990000, 0x23ee0000)
 tenured generation   total 1092288K, used 9438K [0x23ee0000, 0x66990000, 0x66990000)
   the space 1092288K,   0% used [0x23ee0000, 0x24817810, 0x24817a00, 0x66990000)
 compacting perm gen  total 12288K, used 3357K [0x66990000, 0x67590000, 0x6a990000)
   the space 12288K,  27% used [0x66990000, 0x66cd7778, 0x66cd7800, 0x67590000)
No shared spaces configured.
java.lang.OutOfMemoryError: Java heap space
    at org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:147)
    at org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:125)
    at org.apache.poi.hssf.usermodel.HSSFRow.createCell(HSSFRow.java:103)
    at com.test.ExcelWriter.createWorkbook(ExcelWriter.java:119)
    at com.test.TestMe2.main(TestMe2.java:38)
解决方案

try setting both Xms and Xmx to the same value i.e.

-Xms1700m -Xmx1700m

Does this work for you?

public static void main(String[] args) {
  try {
    FileOutputStream fileOut = new FileOutputStream("poi-test.xls");
    HSSFWorkbook workbook = new HSSFWorkbook();
    CreationHelper createHelper = workbook.getCreationHelper();
    HSSFSheet worksheet = workbook.createSheet("POI Worksheet");

    for(int i=0; i<20000; i++) {
      Row row = worksheet.createRow(i);
      row.createCell(0).setCellValue(createHelper.createRichTextString("row " + i));
    }

    fileOut.flush();
    workbook.write(fileOut);
    fileOut.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

这篇关于在写入xls文件与Apache POI堆错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 01:17