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

1 に答える 1

2

ODF スタイルをセルに適用するサンプル コードを次に示します。スタイルを作成するための簡単な解決策が見つかりません。私がしているのは、ods ファイルを作成し、in の子要素をチェックし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");
        }
    }
于 2016-07-09T16:02:49.617 に答える