java POI操作word2010简单实现多级标题结构

java POI操作word2010简单实现多级标题结构_第1张图片

后台生成word后,并导出word时,实现多级标题分级:

//调用方法
XWPFDocument document= new XWPFDocument();
addCustomHeadingStyle(document,“一、级标题”,1);
addCustomHeadingStyle(document,“二、级标题”,2);
addCustomHeadingStyle(document,“三、级标题”,3);
addCustomHeadingStyle(document,“四、级标题”,4);


/**
* 2019-02-12 POI操作word2010实现多级标题结构
* @param docxDocument
* @param strStyleId
* @param headingLevel
*/
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); 

} 

你可能感兴趣的:(疑难问题类)