Оптимизируйте запись файлов с помощью simple-odf
Это мой код для записи моего файла:
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: пространство кучи Java в java.util.ArrayList.iterator(ArrayList.java:814) в sun.nio.ch.WindowsSelectorImpl.updateSelectedKeys(WindowsSelectorImpl.java:496) в sun.nio.or.W.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>
1 ответ
Вот пример кода применения стиля ODF к ячейке. Я не могу найти простое решение для создания стиля. Я создаю файл ods, проверяю дочерний элемент office:automatic-styles
в content.xml
затем преобразовать его в код Java.
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");
}
}