这是我编写文件的代码:
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.util.ArrayList.iterator(ArrayList.java:814),sun.nio.ch.WindowsSelectorImpl.updateSelectedKeys(WindowsSelectorImpl.java:496),sun.nio.ch.WindowsSelectorImpl.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) at 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>发布于 2016-07-09 16:02:49
下面是将ODF样式应用于单元格的示例代码。我找不到一个简单的解决方案来创造风格。我所做的是创建一个ods文件,在content.xml中检查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");
}
}https://stackoverflow.com/questions/37366225
复制相似问题