使用simple-odf优化写入ods文件

时间:2016-05-21 17:45:54

标签: java odftoolkit

这是我编写文件的代码:

    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堆空间at   java.util.ArrayList.iterator(ArrayList.java:814)at   sun.nio.ch.WindowsSelectorImpl.updateSelectedKeys(WindowsSelectorImpl.java:496)     在   sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:172)     at sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:87)at at   sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)at   org.apache.tomcat.util.net.NioEndpoint $ Poller.run(NioEndpoint.java:1050)     在java.lang.Thread.run(Thread.java:745)

如果删除格式(border,font ...),我可以写更多行。 如果我打开content.xml,我可以看到我有许多相同的定义样式。 我正在使用这个版本:

    <dependency>
        <groupId>org.apache.odftoolkit</groupId>
        <artifactId>simple-odf</artifactId>
        <version>0.7-incubating</version>
    </dependency>

1 个答案:

答案 0 :(得分:2)

以下是将ODF样式应用于单元格的示例代码。我找不到一个简单的解决方案来创造风格。我所做的是创建一个ods文件,检查office:automatic-stylescontent.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");
        }
    }
相关问题