当前位置:   article > 正文

2020-12-30_poi更新目录域

poi更新目录域

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

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

/**
	 * 生成word模板
	 */
	public void generaWordfile(HttpServletRequest request, 
			HttpServletResponse response,List<KmsWikiCatelogForm> 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();
	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
2:替换模板并输出
  • 1
public void exportWord( HttpServletRequest request, HttpServletResponse response,List<KmsWikiCatelogForm> 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();
		} 


	}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

三:效果
在这里插入图片描述
四:
菜鸟代码,勿喷。个人觉得简单易懂,有什么疑问可以联系偶。具体效果可以点击头像找到效果视频

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/709502
推荐阅读
相关标签
  

闽ICP备14008679号