这是我编写文件的代码:
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");
在这段代码中,我在单元格级别设置样式。为了优化写作,我想知道是否有任何方法可以用于行或表级别。或者在文档中创建边框、字体、大小等样式,并使用函数 setCellStyleName 设置样式。我可以做这样的事情吗?
原因是因为我收到此错误:
java.lang.OutOfMemoryError:在 sun.nio.ch.WindowsSelectorImpl.updateSelectedKeys(WindowsSelectorImpl.java:496) 在 sun.nio.ch.WindowsSelectorImpl 的 java.util.ArrayList.iterator(ArrayList.java:814) 的 Java 堆空间。 doSelect(WindowsSelectorImpl.java:172) 在 sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87) 在 sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98) 在 org.apache.tomcat.util .net.NioEndpoint$Poller.run(NioEndpoint.java:1050) 在 java.lang.Thread.run(Thread.java:745)
如果我删除格式(边框、字体...),我可以写更多行。如果我打开 content.xml,我可以看到我定义了许多相同的样式。我正在使用这个版本:
<dependency>
<groupId>org.apache.odftoolkit</groupId>
<artifactId>simple-odf</artifactId>
<version>0.7-incubating</version>
</dependency>