poi设置标题格式-大纲标题,自定义标题,用来生成目录

wylc123 16天前 ⋅ 67 阅读

1.使用大纲标题

POI生成Word时,设置段落的样式

String style = "2"; //标题2的样式(这个值,我发现不同的文档值都不一样。。这里只是一个例子)
  XWPFParagraph xwpfParagraph = doc.insertNewParagraph(run);
  xwpfParagraph.setStyle(style);

2. 用户自定义标题格式

2.1 简单的标题样式
2.1.1 自定义方法代码
/**
     * 增加自定义标题样式。这里用的是stackoverflow的源码
     *
     * @param docxDocument 目标文档
     * @param strStyleId 样式名称
     * @param headingLevel 样式级别
     */
    private static void addCustomHeadingStyle(XWPFDocument docxDocument, String strStyleId, int headingLevel) {

        strStyleId = String.valueOf(Integer.parseInt(strStyleId)+1);
        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);

    }
2.1.2 调用方式
public void test() throws Exception {
		int level = 1;
		String styleName = "标题 1";
		String name = "标题内容";
		String filePath = "C:/Users/Desktop/0530测试.docx";
		// 获得word的pack对象
		OPCPackage pack = POIXMLDocument.openPackage( filePath );
		// 获得XWPFDocument对象
		XWPFDocument doc = new XWPFDocument( pack );
		addCustomHeadingStyle( doc, styleName, level );
		XWPFParagraph paragraph = doc.getParagraphs().get( 0 );
		// 段落的格式,下面及个设置,将使新添加的文字向左对其,无缩进.
		paragraph.setIndentationLeft( 0 );
		paragraph.setIndentationHanging( 0 );
		paragraph.setAlignment( ParagraphAlignment.LEFT );
		// paragraph.setWordWrap( true );
		paragraph.setStyle( styleName );
		// 在段落中新插入一个run,这里的run我理解就是一个word文档需要显示的个体,里面可以放文字,参数0代表在段落的最前面插入
		XWPFRun run = paragraph.insertNewRun( 0 );
		// 设置run内容
		run.setText( "中国" );
		run.setFontFamily( "宋体" );
		run.setBold( true );
		run.setFontSize( 20 );
		run.addBreak( BreakType.TEXT_WRAPPING );
		// 生成的标题文件
		File newFile = new File( "C:/Users/Desktop/222.docx" );
		FileOutputStream fos = new FileOutputStream( newFile );
		doc.write( fos );
		fos.flush();
		fos.close();
		pack.close();
		newFile.delete();
	}
2.2 带复杂格式的标题样式
2.2.1 自定义方法代码
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;
}
2.2.2 调用方式
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!");

 


全部评论: 0

    我有话说: