首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用简单-odf优化写ods文件

用简单-odf优化写ods文件
EN

Stack Overflow用户
提问于 2016-05-21 17:45:54
回答 1查看 1.4K关注 0票数 2

这是我编写文件的代码:

代码语言:javascript
复制
    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,我可以看到我有许多定义的样式是相等的。我正在使用这个版本:

代码语言:javascript
复制
    <dependency>
        <groupId>org.apache.odftoolkit</groupId>
        <artifactId>simple-odf</artifactId>
        <version>0.7-incubating</version>
    </dependency>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-07-09 16:02:49

下面是将ODF样式应用于单元格的示例代码。我找不到一个简单的解决方案来创造风格。我所做的是创建一个ods文件,在content.xml中检查content.xml的子元素,然后将其转换为java代码。

代码语言:javascript
复制
    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");
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37366225

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档