简体   繁体   English

使用Apache POI打开电子表格时内存不足

[英]Out of memory when opening a spreadsheet with Apache POI

i am trying to read an excel that is of 50Mb in size and containg various sheets in it through poi in java but the issue is that when i try to creat the object i got the exception of which the stack trace I have posted below. 我试图读取大小为50Mb的Excel,并通过java中的poi在其中包含各种工作表,但是问题是,当我尝试创建对象时,得到了我在下面发布的堆栈跟踪的例外。 for out of memory space error i have configured this 对于内存不足错误,我已对此进行了配置

-Xmx1024m -Duser.timezone=GMT0 already

below is the snapshot in which i am trying to read the excel fist and then convrting it into byte array later on passing it as bytr stream where i detect it extension and it is of .xlsx type so the momnet i try to creat the object i got the below exception please advise how to overcome from this 下面是快照,在其中我尝试读取excel拳头,然后将其作为字节流传递到字节数组中,并在其中检测到扩展名,并且它是.xlsx类型,因此我尝试在momnet中创建对象i遇到以下异常,请告知如何克服此异常

     String fileName = "C:\\abc\\xret.xlsx";


FileInputStream fis = new FileInputStream(fileName);
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
                byte[] bFile = new byte[(int) fileName.length()];
                byte[] buf = new byte[1024];
                try {
                    for (int readNum; (readNum = fis.read(buf)) != -1;) {
                        bos.write(buf, 0, readNum); 
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                byte[] bytes = bos.toByteArray();
                processExcelObjects( bytes);

later on below i am creating the objects further as shown below 稍后在下面,我将进一步创建对象,如下所示

        byteArrayInputStream = new ByteArrayInputStream(bytes);

        if (byteArrayInputStream.available() != -1 )
                { 
                  if (filename.lastIndexOf("."))).equalsIgnoreCase(".xlsx") )
                      {

                     XSSFWorkbook workbookXlsx = new XSSFWorkbook(); 
                       //**** got the exception ********//                       
                     workbookXlsx = new  XSSFWorkbook(byteArrayInputStream); //  ******  on this line I got the exception *********//

                 }

the exception that i got while creating the above workbookXlsx object is 我在创建上述workbookXlsx对象时遇到的异常是

                 org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException
            at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62)
            Caused by: java.lang.OutOfMemoryError: Java heap space
            at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.attr(Cur.java:3039)
            at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.attr(Cur.java:3060)
            Caused by: java.lang.reflect.InvocationTargetException
            at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
            at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)

please advise is it poi internal exception or heap space one 请告知它是poi内部异常还是堆空间之一

It's not a POI internal exception, you did run out of memory. 不是 POI内部异常,您确实耗尽了内存。

You can try to increase to -Xmx2048m or more, but consider a different approach, like the one suggested by @sibnick in the comments: 您可以尝试增加到-Xmx2048m或更多,但可以考虑使用另一种方法,例如@sibnick在评论中建议的方法:

Processing large xlsx file in Java 用Java处理大型xlsx文件

That exception means that your java does not have enough memory to allocate in order to run this. 该异常意味着您的Java没有足够的内存来运行该内存。

can use java -Xms -Xmx on the command line. 可以在命令行上使用java -Xms -Xmx。

If you run the application using Eclipse , 如果您使用Eclipse运行该应用程序,

Right click on project -> Run As -> Run Configurations..-> Select Arguments tab -> In VM Arguments you can increase your JVM memory allocation 右键单击项目->运行方式->运行配置。.->选择参数选项卡->在VM参数中,您可以增加JVM内存分配

refer this documentation for learn more about vmoptions oracle - vmoptions 请参考本文档以了解有关vmoptions oracle的更多信息-vmoptions

I think there is some memory allocation problem you should use 我认为您应该使用一些内存分配问题

for (int readNum; (readNum = fis.read(bFile)) != -1;) {
                        bos.write(bFile, 0, readNum); 
}

replace buf variable to bFile variable and check it. 将buf变量替换为bFile变量并进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用Apache POI填充电子表格模板-内存使用情况 - Filling a spreadsheet template with Apache POI - memory usage Apache Poi:读取电子表格数据时出现异常 - Apache Poi: Exception when reading spreadsheet data Apache Poi Performance在打开包装在jar中的电子表格时出现问题 - Apache Poi Performance issues opening a spreadsheet that is packaged inside a jar 使用 apache POI 读取大型 excel 文件时出现 memory 错误。 任何其他选择 - Out of memory Error When reading a large excel file using apache POI. Any other alternative Apache POI-将.html电子表格转换为.xls电子表格 - Apache POI - Convert .html spreadsheet into .xls spreadsheet apache POI createFormulaListConstraint打开Excel时导致“内容不可读” - apache POI createFormulaListConstraint results in “Unreadable content” when opening excel Apache POI错误打开工作簿 - Apache POI error opening workbook Getting Out of memory when reading large excel files (.xls) using Workbook method of apache poi jxl-2.6.jar - Getting Out of memory when reading large excel files (.xls) using Workbook method of apache poi jxl-2.6.jar 使用Apache Poi读取Excel文件时出现内存问题 - Memory issue when read excel file with Apache poi Apache POI 在电子表格中仅记录 1 行 - Apache POI recording only 1 row in the spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM