SXSSFWorkbook OutOfMemoryException 26 MB文件上的GC开销

时间:2018-11-05 14:39:49

标签: java excel garbage-collection apache-poi out-of-memory

我有一个excel文件,可作为我的程序的模板。该模板文件的大小约为26MB。我尝试使用以下代码打开文件以进行写访问:

Workbook workbook = new SXSSFWorkbook(new XSSFWorkbook(new File(getClass().getResource(TEMPLATE_PATH).getFile())),
                100, true, true);

但这会导致以下异常:

  

java.lang.OutOfMemoryError:超出了GC开销限制   org.apache.xmlbeans.impl.store.Cur $ CurLoadContext.attr(Cur.java:3039)     在org.apache.xmlbeans.impl.store.Locale.loadNode(Locale.java:1433)     在   org.apache.xmlbeans.impl.store.Locale.loadNodeChildren(Locale.java:1396)     在org.apache.xmlbeans.impl.store.Locale.loadNode(Locale.java:1438)     在   org.apache.xmlbeans.impl.store.Locale.loadNodeChildren(Locale.java:1396)     在org.apache.xmlbeans.impl.store.Locale.loadNode(Locale.java:1438)     在   org.apache.xmlbeans.impl.store.Locale.loadNodeChildren(Locale.java:1396)     在org.apache.xmlbeans.impl.store.Locale.loadNode(Locale.java:1438)     在   org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1378)     在   org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1363)     在   org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:370)     在org.apache.poi.POIXMLTypeLoader.parse(POIXMLTypeLoader.java:164)     在   org.openxmlformats.schemas.spreadsheetml.x2006.main.WorksheetDocument $ Factory.parse(未知   来源)   org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:226)在   org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:218)     在   org.apache.poi.xssf.usermodel.XSSFWorkbook.parseSheet(XSSFWorkbook.java:443)     在   org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:408)     在org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:169)处   org.apache.poi.xssf.usermodel.XSSFWorkbook。(XSSFWorkbook.java:270)     在   org.apache.poi.xssf.usermodel.XSSFWorkbook。(XSSFWorkbook.java:323)     在   com.company.project.writer.excel.AirlinesMasterWriter。(AirlinesMasterWriter.java:70)     在   com.company.project.business.controller.impl.DiioBCImpl.writeAirlinesMasterFile(DiioBCImpl.java:141)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)     在   org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)     在   org.springframework.transaction.interceptor.TransactionInterceptor $ 1.proceedWithInvocation(TransactionInterceptor.java:99)     在   org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:280)     在   org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)

使用apache poi写入如此大的excel文件的正确方法是什么?


请注意,在打开模板之前,我获取了需要写入模板的所有数据。数据大小不超过300MB,更可能更少。另外请注意,如果我创建一个新的SXSSFWorkbook而不是打开现有的代码,则其余代码工作正常。

0 个答案:

没有答案