2

这是我编写文件的代码:

    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>
4

1 回答 1

2

这是将 ODF 样式应用于单元格的示例代码。我找不到创建风格的简单解决方案。我所做的是创建一个 ods 文件,检查 in 的子元素,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");
        }
    }
于 2016-07-09T16:02:49.617 回答