Java 使用简单odf优化ods文件的编写

Java 使用简单odf优化ods文件的编写,java,odftoolkit,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.Supporte

这是我编写文件的代码:

    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");
SpreadsheetDocument ods=SpreadsheetDocument.newSpreadsheetDocument();
Table=Table.newTable(ods,4000,20,0,0);
表2.setTableName(“foo”);
Border Border=新边框(Color.BLACK,1,StyleTypeDefinitions.SupportedLinearMeasure.PT);
Font Font=新字体(“Arial”,FontStyle.BOLD,7,Color.BLACK);
列表行=table.getRowList();
用于(r行:行){
对于(int a=0;a<20;a++){
Cell Cell=r.getCellByIndex(a);
单元格设置字符串值(“Foo”+a);
cell.setboorders(cellbordersype.ALL_-FOUR,border);
cell.setCellBackgroundColor(Color.valueOf(“#a5a5”));
cell.setFont(字体);
cell.setHorizontalAlignment(HorizontalAlignmentType.CENTER);
}
}
ods.save(“K://foo.ods”);
在这段代码中,我在单元格级别设置了样式。为了优化写入,我想知道是否有任何方法可以用于行或表级别。或者为边框、字体、大小等创建样式。。。在文档中,使用函数setCellStyleName设置样式。我能做这样的事吗

原因是我遇到了以下错误:

java.lang.OutOfMemoryError:位于的java堆空间 迭代器(ArrayList.java:814) sun.nio.ch.WindowsSelectorImpl.updateSelectedKeys(WindowsSelectorImpl.java:496) 在 sun.nio.ch.WindowsSelectorImpl.doSelect(WindowsSelectorImpl.java:172) 位于sun.nio.ch.SelectorImpl.lock和doselect(SelectorImpl.java:87) sun.nio.ch.SelectorImpl.select(SelectorImpl.java:98)位于 org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:1050) 运行(Thread.java:745)

如果我删除格式(边框、字体…),我可以写更多行。 如果我打开content.xml,我可以看到我定义了许多相同的样式。 我正在使用此版本:

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

org.apache.odftoolkit
简单odf
0.7-孵化

以下是将ODF样式应用于单元格的示例代码。我找不到一个简单的方法来创造风格。我要做的是创建一个ods文件,在
content.xml
中检查
office:automatic styles
的子元素,然后将其转换为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");
        }
    }
SpreadsheetDocument ods=SpreadsheetDocument.newSpreadsheetDocument();
Table=Table.newTable(ods,4000,20,0,0);
表2.setTableName(“foo”);
//创造风格
OdfOfficeAutomaticStyles astyles=ods.getContentDom().getOrCreateAutomaticStyles();
styleelement ele=astyles.newstyleelement(OdfStyleFamily.TableCell.getName(),“myss”);
StyleTableCellPropertiesElement StyleTableCellPropertiesElement=ele.newStyleTableCellPropertiesElement();
StyleTableCellPropertieElement.setFoBackgroundColorAttribute(“#A5A5A5A5A5”);
StyleTableCellPropertieElement.setFoBorderAttribute(“1.0pt固体#000000”);
ele.newStyleParagraphPropertieElement().setFoTextAlignAttribute(HorizontalAlignmentType.CENTER.toString());
StyleTextPropertiesElement StyleTextPropertiesElement=ele.newStyleTextPropertiesElement(null);
StyleTextPropertieElement.setStyleFontNameAttribute(“Arial”);
StyleTextPropertieElement.setFontSizeAttribute(“7.0pt”);
StyleTextPropertieElement.setFoColorAttribute(Color.BLACK.toString());
styleTextPropertieElement.setFontWeightAttribute(“粗体”);
列表行=table.getRowList();
用于(r行:行){
对于(int a=0;a<10;a++){
Cell Cell=r.getCellByIndex(a);
单元格设置字符串值(“Foo”+a);
cell.setCellStyleName(“myss”);
}
}

没有使用简单odf的经验,但是使用Document、getOrCreateDocumentStyle。此外,重复创建新实例(如Color.valueOf)也会影响内存。您是否尝试给程序多一点内存?@Joop Eggen感谢您提供的信息。我在循环外定义了一个颜色,这样color color=color.valueOf(“#a5a5”);但是我得到了同样的错误,我更改了STS.ini中的值(我使用Spring工具套件),但没有任何更改。我的值:-vmargs-Dosgi.requiredJavaVersion=1.7-Xms512m-XX:MaxPermSize=512m-Dorg.eclipse.swt.browser.IEVersion=10001-Xmx1024m@RC。