XWPFDocument 创建Word并且生成目录结构

  //生成目录结构时  需要这个功能 已达到生成下标的作用
public static void createDefaultFooter(final XWPFDocument docx) {
	    // TODO 设置页码起始值
	    CTP pageNo = CTP.Factory.newInstance();
	    XWPFParagraph footer = new XWPFParagraph(pageNo, docx);
	    CTPPr begin = pageNo.addNewPPr();
	    begin.addNewPStyle().setVal("系统页脚");
	    begin.addNewJc().setVal(STJc.CENTER);
	    pageNo.addNewR().addNewFldChar().setFldCharType(STFldCharType.BEGIN);
	    pageNo.addNewR().addNewInstrText().setStringValue("PAGE   \\* MERGEFORMAT");
	    pageNo.addNewR().addNewFldChar().setFldCharType(STFldCharType.SEPARATE);
	    CTR end = pageNo.addNewR();
	    CTRPr endRPr = end.addNewRPr();
	    endRPr.addNewNoProof();
	    endRPr.addNewLang().setVal("123");
	    end.addNewFldChar().setFldCharType(STFldCharType.END);
	    CTSectPr sectPr = docx.getDocument().getBody().isSetSectPr() ? 
        docx.getDocument().getBody().getSectPr()/docx.getDocument().getBody().addNewSectPr();
	    XWPFHeaderFooterPolicy policy = new XWPFHeaderFooterPolicy(docx, sectPr);
	    policy.createFooter(STHdrFtr.DEFAULT, new XWPFParagraph[] { footer });
	}



 /**
    * 将 该paragraph 设置成Word中的一级 二级标签
    * 这里设置完成后 document.createTOC  就能够显示 目录结构
    * @param paragraph
    */
public static void createLevel(XWPFDocument document,String style,
		ParagraphAlignment para_align,String color,Integer font_size,
		String content,boolean bold){
	XWPFParagraph paragraph = document.createParagraph();
	
	
	XWPFStyles styles = document.createStyles();
	if (!StringUtils.isEmpty(style)) {
		Integer level = Integer.parseInt(style.split("heading")[1]);
		addCustomHeadingStyle(document, styles, style, level, 36, "4288BC");	
	}
	paragraph.setAlignment(para_align);
	paragraph.setStyle(style);
	XWPFRun r = paragraph.createRun();
	r.setColor(color);
	r.setFontSize(font_size);
	r.setText(content);
	r.setBold(bold);
} 


//设置 一二级标签
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);
}

//在你的文档生成代码中   需要的标题如下设置
createLevel(doc, "heading1", ParagraphAlignment.CENTER, "", 22, "第一章  项目创意", true);//一级标签

createLevel(doc, "heading1", ParagraphAlignment.CENTER, "", 22, "1、项目分析", true);//二级标签


最后

WordExportUtil.createDefaultFooter(doc); 
//创建目录  需要上面的 下标
doc.createTOC();
request.setCharacterEncoding("utf-8");  
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
 new String( ( project.getTitle()+"-创业计划书.doc").getBytes(),  
        "iso-8859-1"));
OutputStream ostream = response.getOutputStream();
doc.write(ostream); 
ostream.close();


//生成文档后 打开(以Word->Office)启动编辑 会有提示,选择 是,会自动显示目录结构
//以上就像成了目录结构
第一章
  1、项目分析








 

 

打开后点击“是”-》》  编辑模式打开 也选择是  这样就OK了

XWPFDocument 创建Word并且生成目录结构_第1张图片

 

 

你可能感兴趣的:(web,java,wordpress)