如何设置标题样式在使用Apache POI的DOCX文件的一个段落?-开发者之家 - 亲测可用

问题描述

我想创建一个POI的docx文件,但我不能设置标题样式为一个段落。

I am trying to create a docx file with poi but I cannot set heading style for a paragraph.

XWPFDocument document= new XWPFDocument(); 

//Write the Document in file system
FileOutputStream out = new FileOutputStream(new File("C:/Users/2/Desktop/RequirementModelDocument.docx"));

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();


paragraph.setAlignment(ParagraphAlignment.LEFT);
paragraph.setStyle("Heading1");

run.setText(reqLevel.getName());
run.setBold(true);
run.setFontFamily("Calibri Light (Headings)");

这就像忽略 paragraph.setStyle(标题1); 行。我看了看apache的例子,但我看不出这个问题的例子。

Its like ignores the paragraph.setStyle("Heading1"); line. I've looked at the apache's examples but I could not see any example about this issue.

推荐答案

如果你开始一个新的文档,没有定义的样式。无论是从现有模板开始并复制样式,或创建自己的(像我一样,基于 http://stackoverflow.com/ A /四十六万一千四百九十九分之二千七百八十六万四千七百五十二)

If you start a new document, there are no style defined. Either start from an existing template and copy the styles, or create your own (like I did, based on http://stackoverflow.com/a/27864752/461499 )

在这里看到:

    XWPFDocument document = new XWPFDocument();
    XWPFStyles styles = document.createStyles();

    String heading1 = "My Heading 1";
    String heading2 = "My Heading 2";
    String heading3 = "My Heading 3";   
    String heading4 = "My Heading 4";
    addCustomHeadingStyle(document, styles, heading1, 1, 36, "4288BC");
    addCustomHeadingStyle(document, styles, heading2, 2, 28, "4288BC");
    addCustomHeadingStyle(document, styles, heading3, 3, 24, "4288BC");
    addCustomHeadingStyle(document, styles, heading4, 4, 20, "000000");

    XWPFParagraph paragraph = document.createParagraph();
    paragraph.setStyle(heading1);
    XWPFRun run = paragraph.createRun();
    run.setText("Nice header!");

private static void addCustomHeadingStyle(XWPFDocument docxDocument, XWPFStyles styles, String strStyleId, int headingLevel, int pointSize, String hexColor) {

    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);

    CTHpsMeasure size = CTHpsMeasure.Factory.newInstance();
    size.setVal(new BigInteger(String.valueOf(pointSize)));
    CTHpsMeasure size2 = CTHpsMeasure.Factory.newInstance();
    size2.setVal(new BigInteger("24"));

    CTFonts fonts = CTFonts.Factory.newInstance();
    fonts.setAscii("Loma" );

    CTRPr rpr = CTRPr.Factory.newInstance();
    rpr.setRFonts(fonts);
    rpr.setSz(size);
    rpr.setSzCs(size2);

    CTColor color=CTColor.Factory.newInstance();
    color.setVal(hexToBytes(hexColor));
    rpr.setColor(color);
    style.getCTStyle().setRPr(rpr);
    // is a null op if already defined

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

}

public static byte[] hexToBytes(String hexString) {
     HexBinaryAdapter adapter = new HexBinaryAdapter();
     byte[] bytes = adapter.unmarshal(hexString);
     return bytes;
}

原网址: 访问
创建于: 2023-03-21 15:49:33
目录: default
标签: 无

请先后发表评论
  • 最新评论
  • 总共0条评论