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