2014-05-16 61 views
0

我想通過java web apache poi在包含大約8000列和1200行的netbeans中讀取一個excel文件,其中我收到以下異常。我也嘗試使用-Xmx2048m來增加netbeans中的堆大小,但它無濟於事。在java中讀取一個大的excel文件

Exception in thread "main" 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) 
    at org.apache.xmlbeans.impl.store.Locale$SaxHandler.startElement(Locale.java:3250) 
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportStartTag(Piccolo.java:1082) 
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseAttributesNS(PiccoloLexer.java:1802) 
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseOpenTagNS(PiccoloLexer.java:1521) 
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseTagNS(PiccoloLexer.java:1362) 
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseXMLNS(PiccoloLexer.java:1293) 
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseXML(PiccoloLexer.java:1261) 
    at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.yylex(PiccoloLexer.java:4808) 
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yylex(Piccolo.java:1290) 
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yyparse(Piccolo.java:1400) 
    at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:714) 
    at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3439) 
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1270) 
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1257) 
    at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345) 
    at org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument$Factory.parse(Unknown Source) 
    at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:188) 
    at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:180) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:300) 
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159) 
    at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:221) 
    at testdoc.Testdoc.main(Testdoc.java:26) 
Java Result: 1 
BUILD SUCCESSFUL (total time: 49 seconds) 

行無26

File excel = new File ("E:\\Project\\Rapid out\\"+filename+""+type+".xlsx"); 
       FileInputStream fis = new FileInputStream(excel); 
     Line 26:: XSSFWorkbook wb = new XSSFWorkbook(fis); 
       XSSFSheet ws = wb.getSheet("Sheet2"); 
+0

我認爲[excel]標籤本身不會對你有任何好處。 – pnuts

回答

1

而不是使用InputStream的,你可以嘗試使用File:

XSSFWorkbook wb = new XSSFWorkbook(excel);

POI Guide

打開工作簿時,無論是.xls HSSFWorkbook還是.xlsx XSSFWorkbook,都可以從File或InputStream加載工作簿。使用File對象可以降低內存消耗,而InputStream需要更多內存,因爲它必須緩衝整個文件。