简单的ODS,OutOfMemoryError:保存文件

时间:2016-05-10 14:45:19

标签: java spring export ods

当保存1500行或更多行的文件时,我收到此错误“OutOfMemoryError:Java堆空间”。

我尝试解决这个拆分列表数据

我尝试将此拆分数据列表解析为更小的部分,并保存并加载ods文件。我使用de function insertRowsBefore来追加新行。但它不适合我。当保存加载表时,附加新的行列表并保存,保存功能再次保存一个完整的表,我得到错误。我的行有三十五列

我尝试使用此版本:

<dependency>
<groupId>org.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.6.6</version>
</dependency>

<dependency>
<groupId>org.apache.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.7-incubating</version>
</dependency>

错误详情:

13:45:12,821 INFO [stdout] Excepción: java.lang.OutOfMemoryError: Java heap space 
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.util.Arrays.copyOf(Arrays.java:2271) 
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
13:45:12,822 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125) 
13:45:12,823 INFO [stdout] (asyncExecutor-1) at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
13:45:12,823 INFO [stdout] (asyncExecutor-1) at java.io.Writer.write(Writer.java:127)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.Printer.printText(Unknown Source)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.printAttribute(Unknown Source)
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.serializeElement(Unknown Source) 
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source) 
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.serializeElement(Unknown Source) 
13:45:12,824 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source) 
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.XMLSerializer.serializeElement(Unknown Source) 
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source) 
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serializeNode(Unknown Source) 
13:45:12,825 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.BaseMarkupSerializer.serialize(Unknown Source) 
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.apache.xml.serialize.DOMSerializerImpl.write(Unknown Source)
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.flushDom(OdfPackage.java:1626)
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.getBytes(OdfPackage.java:1566)
13:45:12,826 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.save(OdfPackage.java:877)
13:45:12,827 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackage.save(OdfPackage.java:823)
13:45:12,827 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.simple.Document.save(Document.java:728) 
13:45:12,827 INFO [stdout] (asyncExecutor-1) at org.odftoolkit.odfdom.pkg.OdfPackageDocument.save(OdfPackageDocument.java:228)

记录我的自定义功能和服务

13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
13:45:12,828 INFO [stdout] (asyncExecutor-1) | java.lang.OutOfMemoryError: Java heap space 
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.util.Arrays.copyOf(Arrays.java:2271)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:113)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140)
13:45:12,828 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
13:45:12,829 INFO [stdout] (asyncExecutor-1) at java.io.Writer.write(Writer.java:127)

我的导出代码:

public static <T> void export(String[] columnTitle, List<T> data, String[] fields, String path, String filename) throws Exception {

    try {
        SpreadsheetDocument ods = SpreadsheetDocument.newSpreadsheetDocument();
        ods.removeSheet(0);

        Table table = Table.newTable(ods, data.size() + MagicNumber.TWO, columnTitle.length, 0, 0);
        table.setTableName(filename);
        String[] values = new String[fields.length];
        int line = 0;

        Border border = new Border(Color.BLACK, BORDER_WIDTH, StyleTypeDefinitions.SupportedLinearMeasure.PT);

        List<Row> rows = table.getRowList();

        Row r = rows.get(line);
        Cell cell = r.getCellByIndex(0);

        //Style for column title
        Font font = new Font(ARIAL_FONT, FontStyle.BOLD, MagicNumber.SEVEN, Color.BLACK);
        r = rows.get(line);
        for (int a = 0; a < fields.length; a++) {
            cell = r.getCellByIndex(a);
            cell.setStringValue(columnTitle[a]);
            cell.setBorders(CellBordersType.ALL_FOUR, border);
            cell.setCellBackgroundColor(Color.valueOf("#A5A5A5"));
            cell.setFont(font);
            cell.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
        }

        //write data
        font = new Font(ARIAL_FONT, FontStyle.REGULAR, MagicNumber.SEVEN);
        for (T obj : data) {
            line++;
            r = rows.get(line);
            for (int i = 0; i < fields.length; i++) {
                cell = r.getCellByIndex(i);
                values[i] = getProperty(obj, fields[i]);
                cell.setStringValue(String.valueOf(values[i]));
                cell.setBorders(CellBordersType.ALL_FOUR, border);
                cell.setFont(font);
                cell.setHorizontalAlignment(HorizontalAlignmentType.RIGHT);
            }
        }

        //Error with 2000 results
        ods.save(path);

    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
        LOG.error(e);
    } catch (Exception e) {
        LOG.error(e);            
    }
}

1 个答案:

答案 0 :(得分:0)

在得到GC OutOfMemory错误之前,我必须编写200000行。

为了解决这个问题,我在for循环中每1000行添加一次保存(下面的SAVE_POINT)

for (T obj : data) {
    line++;
    r = rows.get(line);

    for (int i = 0; i < fields.length; i++) {
        cell = r.getCellByIndex(i);
        values[i] = getProperty(obj, fields[i]);
        cell.setStringValue(String.valueOf(values[i]));
        cell.setBorders(CellBordersType.ALL_FOUR, border);
        cell.setFont(font);
        cell.setHorizontalAlignment(HorizontalAlignmentType.RIGHT);
    }

    if ((line % SAVE_POINT) == 0) {
        ods.save(path + filename);
    }

}

我在一些论坛中读到过简单的odf对于大型电子表格不太好,所以如果你有任何进一步的问题,可以尝试另一个像POI这样的库。