首页 > 其他 > 详细

word模板批量导出

时间:2019-12-03 11:14:17      阅读:141      评论:0      收藏:0      [点我收藏+]
//根据业务传入需要ids
			if(ids!=null && ids.size()>0){
				//放置多模板
				List<XWPFTemplate> temList = new ArrayList<XWPFTemplate>();
				//模板存放路径
			    String uploadFileBasePath = ConfigUtils.getConfigValue("template_path","");
			    //获取首页标题及日期
				final String docname = UserUtils.getUser().getCompany().getName().concat("安全生产责任书");
				final String exportdate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
			//第一页模板数据填充
			XWPFTemplate template1 = XWPFTemplate.compile(uploadFileBasePath+"ResponsibilityBook1.docx").render(new HashMap<String, Object>(){{  
		        put("docname", docname);
		        put("exportdate", exportdate);
		        
		}});
			//添加模板
			temList.add(template1);
			ArrayList<XWPFDocument> documentList = new ArrayList<XWPFDocument>();
			//获取第二个模板填充数据
			for (String id : ids) {
			responsibilityBook=responsibilityBookService.get(id);
			//初始化安全生产目标内容
			responsibilityBook = responsibilityBookService.initContent(responsibilityBook);
			final String bookname = UserUtils.getUser().getCompany().getName().concat(responsibilityBook.getRoleName()).concat("安全生产责任书");
			final String foreword = StringEscapeUtils.unescapeHtml4(responsibilityBook.getForeword());
			final String assessmentmethod = StringEscapeUtils.unescapeHtml4(responsibilityBook.getAssessmentMethod());
			String temp = "";
			
			//封装安全生产目标
			JSONArray jsonArray = JSONArray.parseArray(StringEscapeUtils.unescapeHtml4(responsibilityBook.getGoals()));
			Content entity_temp = null;
			for (Object jsonObject : jsonArray) {
				entity_temp = JSON.parseObject(jsonObject.toString(), Content.class);
				if(jsonArray.indexOf(jsonObject)>0)
					temp += "    ".concat(String.valueOf(jsonArray.indexOf(jsonObject)+1)).concat("、").concat(entity_temp.getContent()).concat("\r\n");
				else
					temp += String.valueOf(jsonArray.indexOf(jsonObject)+1).concat("、").concat(entity_temp.getContent()).concat("\r\n");
			}
			final String goals  = temp;
			
			//封装安全生产责任制
			temp  = "";
			jsonArray = JSONArray.parseArray(StringEscapeUtils.unescapeHtml4(responsibilityBook.getResponsibilitys()));
			entity_temp = null;
			for (Object jsonObject : jsonArray) {
				entity_temp = JSON.parseObject(jsonObject.toString(), Content.class);
				if(jsonArray.indexOf(jsonObject)>0)
					temp += "    ".concat(String.valueOf(jsonArray.indexOf(jsonObject)+1).concat("、")).concat(entity_temp.getContent()).concat("\r\n");
				else
					temp += String.valueOf(jsonArray.indexOf(jsonObject)+1).concat("、").concat(entity_temp.getContent()).concat("\r\n");
			}
			final String responsibilitys = temp;
			
			//第二个模板数据填充
			XWPFTemplate template2 = XWPFTemplate.compile(uploadFileBasePath+"ResponsibilityBook2.docx").render(new HashMap<String, Object>(){{  
			        put("bookname", bookname);
			        put("foreword", foreword);
			        put("goals", goals);
			        put("responsibilitys", responsibilitys);
			        put("assessmentmethod", assessmentmethod);
			}});
			//放入第二个模板
			temList.add(template2);
			}
			
		
			XWPFDocument doc = null;
			for (int i = 0; i < temList.size(); i++) {
				//获取第一个document
				doc = temList.get(0).getXWPFDocument();
				//往第一个document后拼接之后的document
				if (i != 0) {
					CTBody src = doc.getDocument().getBody();
					CTBody append = temList.get(i).getXWPFDocument().getDocument().getBody();
					CombineWord.appendBody(src, append);
					//分页 功能
					//doc.createParagraph().setPageBreak( true );
				}
			}
			
			response.reset();
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename="+Encodes.urlEncode(docname));
    		doc.write(response.getOutputStream()); 
			doc.close();
			}

  word模板1

技术分享图片

 

 模板2

技术分享图片

 

 技术分享图片

 

 

word模板批量导出

原文:https://www.cnblogs.com/wjlwo2ni/p/11975076.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!