2

This is my code to write my file:

    SpreadsheetDocument ods = SpreadsheetDocument.newSpreadsheetDocument();
    Table table = Table.newTable(ods, 4000, 20, 0, 0);
    table.setTableName("foo");
    Border border = new Border(Color.BLACK, 1, StyleTypeDefinitions.SupportedLinearMeasure.PT);
    Font font = new Font("Arial", FontStyle.BOLD, 7, Color.BLACK);
    List<Row> rows = table.getRowList();

    for (Row r : rows) {
        for (int a = 0; a < 20; a++) {
            Cell cell = r.getCellByIndex(a);
            cell.setStringValue("Foo " + a);
            cell.setBorders(CellBordersType.ALL_FOUR, border);
            cell.setCellBackgroundColor(Color.valueOf("#A5A5A5"));
            cell.setFont(font);
            cell.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
        }
    }

    ods.save("K://foo.ods");

In this code I set the style at the cell level. To optimize the writing I want to know if there is any way to do for row or table level. Or create a style for border, font, size, etc ... in the document and set style with function setCellStyleName. I can do something like this?

The reason is because I get this error:

java.lang.OutOfMemoryError: Java heap space at java.util.ArrayList.iterator(ArrayList.java:814) at sun.nio.ch.WindowsSelectorImpl.updateSelectedKeys(WindowsSelectorImpl.java:496) at sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:172) at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87) at sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98) at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:1050) at java.lang.Thread.run(Thread.java:745)

If I delete the format (border, font ...), I can write more rows. If I open the content.xml, I can see that I have many defined styles that are equal. I'm using this version:

    <dependency>
        <groupId>org.apache.odftoolkit</groupId>
        <artifactId>simple-odf</artifactId>
        <version>0.7-incubating</version>
    </dependency>
kryger
  • 12,906
  • 8
  • 44
  • 65
oscar
  • 1,636
  • 6
  • 31
  • 59
  • Not experienced with simple odf, but Document,getOrCreateDocumentStyle. Also repeatedly creating new instances, like Color.valueOf have an impact on memory. – Joop Eggen May 21 '16 at 18:28
  • Did you try to give the program a little bit more memory? –  May 21 '16 at 18:30
  • @Joop Eggen Thanks for your info. I define a color once outside loop, this way Color color = Color.valueOf("#A5A5A5"); but i get the same error. – oscar May 21 '16 at 18:43
  • I change the values in my STS.ini (I use Spring tool suite) but nothing changes. My values: -vmargs -Dosgi.requiredJavaVersion=1.7 -Xms512m -XX:MaxPermSize=512m -Dorg.eclipse.swt.browser.IEVersion=10001 -Xmx1024m @RC. – oscar May 21 '16 at 18:48

1 Answers1

2

Here is the sample code of apply ODF style to cell. I cannot find a easy solution to create style. What I do is createing a ods file, check the child element of office:automatic-styles in content.xml then convert it to java code.

    SpreadsheetDocument ods = SpreadsheetDocument.newSpreadsheetDocument();
    Table table = Table.newTable(ods, 4000, 20, 0, 0);
    table.setTableName("foo");
    //create style
    OdfOfficeAutomaticStyles astyles = ods.getContentDom().getOrCreateAutomaticStyles();
    StyleStyleElement ele = astyles.newStyleStyleElement(OdfStyleFamily.TableCell.getName(), "myss");
    StyleTableCellPropertiesElement styleTableCellPropertiesElement = ele.newStyleTableCellPropertiesElement();
    styleTableCellPropertiesElement.setFoBackgroundColorAttribute("#A5A5A5");
    styleTableCellPropertiesElement.setFoBorderAttribute("1.0pt solid #000000");
    ele.newStyleParagraphPropertiesElement().setFoTextAlignAttribute(HorizontalAlignmentType.CENTER.toString());
    StyleTextPropertiesElement styleTextPropertiesElement = ele.newStyleTextPropertiesElement(null);
    styleTextPropertiesElement.setStyleFontNameAttribute("Arial");
    styleTextPropertiesElement.setFoFontSizeAttribute("7.0pt");
    styleTextPropertiesElement.setFoColorAttribute(Color.BLACK.toString());
    styleTextPropertiesElement.setFoFontWeightAttribute("bold");

    List<Row> rows = table.getRowList();
    for (Row r : rows) {
        for (int a = 0; a < 10; a++) {
            Cell cell = r.getCellByIndex(a);
            cell.setStringValue("Foo " + a);
            cell.setCellStyleName("myss");
        }
    }
Beck Yang
  • 3,004
  • 2
  • 21
  • 26