在java中读取一个大的excel文件

时间:2014-05-16 18:54:34

标签: excel

我正在尝试通过netbeans中的java apache poi读取一个excel文件,其中包含大约8000列和1200行,我得到以下异常。我也尝试用-Xmx2048m增加netbeans中的堆大小,但它对我没有帮助。

    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");

1 个答案:

答案 0 :(得分:1)

您可以尝试使用File:

,而不是使用InputStream
  

XSSFWorkbook wb = new XSSFWorkbook(excel);

来自POI Guide

  

打开工作簿(.xls HSSFWorkbook或.xlsx XSSFWorkbook)时,可以从File或InputStream加载工作簿。使用File对象可以降低内存消耗,而InputStream需要更多内存,因为它必须缓冲整个文件。