如何在带有POI的DOCX中使用预定义格式?

gue*_*rda 14 java docx apache-poi xwpf

我正在使用POI创建一个docx生成器,并希望使用预定义的格式.

Word包括多种格式,如标题,标题1..10等.这些格式是在您使用Word创建的每个DOCX中预定义的.

我想在我的docx生成器中使用它们.我尝试了以下但是格式没有应用:

paragraph = document.createParagraph();
lastParagraph.setStyle("Heading1");
Run Code Online (Sandbox Code Playgroud)

我还尝试了"标题1","heading1"和"Heading1"作为样式,但它们都没有奏效.
API文档不显示任何信息.

我分析了用Word 2007创建的docx文件,发现"Heading1"是正确的.不幸的是,样式没有在docx中定义.我是否必须手动创建此样式?

谁能指出我正确的解决方案?

gue*_*rda 19

这很简单:使用"模板"docx文件.

  1. 使用Word 2007创建一个空的docx文件.
  2. 使用此文件作为您的模板 XWPFDocument
  3. 添加样式的段落.

这是代码:

XWPFDocument document = new XWPFDocument(new FileInputStream("template.docx");
paragraph = document.createParagraph();
paragraph.setStyle("Heading1");
Run Code Online (Sandbox Code Playgroud)

该模板包含所有样式,因此可以通过引用setStyle("Heading1");.

  • 这很棒!但是有一点需要注意......似乎单词会丢弃模板文档中未使用的样式.您需要使用这些样式在文档中包含现有文本,或以某种方式使用模板... (4认同)

Dre*_*ejc 9

您可以创建一个单词模板(只需使用Word中的另存为...功能).

第一种选择

该模板现在在\ word文件夹中包含许多其他XML文件: - styles.xml - stylesWithEffects.xml - webSettings.xml - fontTable.xml和 - \theme文件夹

如果将这些文件复制到原始POI生成的文件中,则可以引用styles.xml文件中给出的样式.

您可以像ZIP文件一样操作原始文件,这不应该花费太多精力.

第二种选择

将代码中的样式从模板复制到文档中:

XWPFDocument template = new XWPFDocument(new FileInputStream(new File("Template.dotx")));       

XWPFDocument doc = new XWPFDocument();      
// let's copy styles from template to new doc
XWPFStyles newStyles = doc.createStyles();
newStyles.setStyles(template.getStyle());


XWPFParagraph para = doc.createParagraph();
para.setStyle("Heading1");

XWPFRun run = para.createRun();
run.setText("Heading 1");

return doc;
Run Code Online (Sandbox Code Playgroud)

从好的方面来说,您可以使用Word直接单独操作样式并将其保存回模板文件.


Rob*_*rtG 8

如果您通常对创建被识别为1级标题的样式感兴趣(例如,用于MS Word生成的TOC)并且可以在Word格式栏中访问,则可以这样执行:

private static File writeSimpleDocxFile(String content) throws IOException {
    XWPFDocument docxDocument = new XWPFDocument();

    String strStyleId = "ownstyle1";

    addCustomHeadingStyle(docxDocument, strStyleId, 1);

    XWPFParagraph paragraph = docxDocument.createParagraph();
    XWPFRun run = paragraph.createRun();
    run.setText(content);

    paragraph.setStyle(strStyleId);
}

private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {

    CTStyle ctStyle = CTStyle.Factory.newInstance();
    ctStyle.setStyleId(strStyleId);

    CTString styleName = CTString.Factory.newInstance();
    styleName.setVal(strStyleId);
    ctStyle.setName(styleName);

    CTDecimalNumber indentNumber = CTDecimalNumber.Factory.newInstance();
    indentNumber.setVal(BigInteger.valueOf(headingLevel));

    // lower number > style is more prominent in the formats bar
    ctStyle.setUiPriority(indentNumber);

    CTOnOff onoffnull = CTOnOff.Factory.newInstance();
    ctStyle.setUnhideWhenUsed(onoffnull);

    // style shows up in the formats bar
    ctStyle.setQFormat(onoffnull);

    // style defines a heading of the given level
    CTPPr ppr = CTPPr.Factory.newInstance();
    ppr.setOutlineLvl(indentNumber);
    ctStyle.setPPr(ppr);

    XWPFStyle style = new XWPFStyle(ctStyle);

    // is a null op if already defined
    XWPFStyles styles = docxDocument.createStyles();

    style.setType(STStyleType.PARAGRAPH);
    styles.addStyle(style);

}
Run Code Online (Sandbox Code Playgroud)

是的,此样式将显示在styles.xml中.

(我知道:这不是你问题的直接答案,但由于我没有在互联网上以可用的形式找到这些信息,我会在这里发布)