2020-12-30

## 使用poi生成带目录的word,且目录和正文锚点关联

一:思路
使用poi生成word模板,然后用easypoi替换模板。
二:具体操作
1:生成word模板,代码如下:

/**
	 * 生成word模板
	 */
	public void generaWordfile(HttpServletRequest request, 
			HttpServletResponse response,List listform,String wordtitle) throws Exception{
		XWPFDocument document= new XWPFDocument();

		File file = new File("");
		String filePath = file.getCanonicalPath();

		FileOutputStream out = new FileOutputStream(new File(filePath+"\\createTemplate.docx"));

		//添加标题
		XWPFParagraph titleParagraph = document.createParagraph();

		//设置段落居中
		titleParagraph.setAlignment(ParagraphAlignment.CENTER);

		XWPFRun titleParagraphRun = titleParagraph.createRun();
		CTFonts fontt = titleParagraphRun.getCTR().addNewRPr().addNewRFonts();
		fontt.setEastAsia("宋体");
		fontt.setAscii("宋体");
		titleParagraph.setSpacingAfter(360);
		titleParagraphRun.setText("{
    {wordtitle}}");
		titleParagraphRun.setColor("000000");
		titleParagraphRun.setFontSize(18);
		titleParagraphRun.setBold(true);

		XWPFParagraph titleParagraphm = document.createParagraph();
		titleParagraphm.setAlignment(ParagraphAlignment.CENTER);
		XWPFRun titleParagraphRunm = titleParagraphm.createRun();
		titleParagraphm.setSpacingAfter(360);
		CTFonts font = titleParagraphRunm.getCTR().addNewRPr().addNewRFonts();
		font.setEastAsia("黑体");
		font.setAscii("黑体");
		titleParagraphRunm.setFontSize(18);
		titleParagraphRunm.setBold(true);
		titleParagraphRunm.setText("【目录】");

        
		for (int i = 0; i < listform.size(); i++) {
			XWPFParagraph titleParagraph1 = document.createParagraph();
			titleParagraph1.setAlignment(ParagraphAlignment.LEFT);
			//titleParagraph1.setSpacingAfter(200);
			CTPPr ppr = titleParagraph1.getCTP().getPPr();
	        if (ppr == null){ 
	        	ppr = titleParagraph1.getCTP().addNewPPr();
	        }
	        CTSpacing spacing = ppr.isSetSpacing() ? ppr.getSpacing() : ppr.addNewSpacing();
	        spacing.setAfter(BigInteger.valueOf(0));
	        spacing.setBefore(BigInteger.valueOf(0));
	        spacing.setLineRule(STLineSpacingRule.AUTO);
	        spacing.setLine(BigInteger.valueOf(360));
	        
			
			
			XWPFRun titleParagraphRun1 = titleParagraph1.createRun();
			
			String arr [] =listform.get(i).getFdCateIndex().split("\\.");
			
			String catalog="{
    {catalog"+i+"}}";
			int fontsize = 18-arr.length*2;

			/*-------------*/
			XWPFHyperlinkRun hyperlinkrun = createHyperlinkRunToAnchor(titleParagraph1, "{
    {title"+i+"}}");
	        hyperlinkrun.setText(catalog);
	        hyperlinkrun.setFontSize(fontsize);
	        hyperlinkrun.setBold(true);

	        /*-------------*/
	        CTFonts font1 = hyperlinkrun.getCTR().addNewRPr().addNewRFonts();
	        if(arr.length==1){
				font1.setEastAsia("黑体");
				font1.setAscii("黑体");
			}else{
				if(listform.get(i).getDocContent()!=null){
					hyperlinkrun.setFontSize(11);
					font1.setEastAsia("仿宋");
					font1.setAscii("仿宋");
				}else{
					font1.setEastAsia("宋体");
					font1.setAscii("宋体");
				}
			}
		}
		
		XWPFParagraph paragraph2 = document.createParagraph();
        //设为横向
        CTPPr ctpPr = paragraph2.getCTP().addNewPPr();
        //添加分节符
        CTSectPr sectPr = ctpPr.addNewSectPr();
		
		for (int i = 0; i < listform.size(); i++) {
			String arr [] =listform.get(i).getFdCateIndex().split("\\.");
			int fontsize = 18-arr.length*2;
			String t="{
    {title"+i+"}}";
			String c=null;
			if(listform.get(i).getDocContent() != null && !"".equals(replaceNbsp(listform.get(i).getDocContent()).trim())){
				c="{
    {content"+i+"}}";
			}
			createContent(t,c,document,fontsize,i,listform.get(i).getDocContent() != null);
		}
		createDefaultFooter(document);
		document.write(out);
		out.close();
	}
2:替换模板并输出
public void exportWord( HttpServletRequest request, HttpServletResponse response,List listform,String wordtitle) throws Exception{

		try {
			//String template = "/com/landray/kmss/kms/%s/%s";
			File file = new File("");
			String template = file.getCanonicalPath();
			InputStream is = null;
			is = new FileInputStream(template+"\\createTemplate.docx");   
			MyXWPFDocument doc = new MyXWPFDocument(is);
			WordExportUtil.exportWord07(doc, getMap(listform,wordtitle));
			String datastr=DateUtil.convertDateToString(new Date(),"yyyyMMddHHmm");
			response.setContentType("application/force-download");
			// 文件名
			response.addHeader("Content-Disposition", "attachment;fileName=" +datastr+".docx");
			OutputStream out = response.getOutputStream();
			doc.write(out);
			
			File f=new File(template+"\\createTemplate.docx");
			if(f.exists()){
				f.delete();
			}
			out.close();
			close(is);

		} catch (Exception e) {
			e.printStackTrace();
		} 


	}

三:效果
2020-12-30_第1张图片
四:
菜鸟代码,勿喷。个人觉得简单易懂,有什么疑问可以联系偶。具体效果可以点击头像找到效果视频

你可能感兴趣的:(poi)