zoukankan      html  css  js  c++  java
  • word模板批量导出

    //根据业务传入需要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("
    ");
    				else
    					temp += String.valueOf(jsonArray.indexOf(jsonObject)+1).concat("、").concat(entity_temp.getContent()).concat("
    ");
    			}
    			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("
    ");
    				else
    					temp += String.valueOf(jsonArray.indexOf(jsonObject)+1).concat("、").concat(entity_temp.getContent()).concat("
    ");
    			}
    			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

     

  • 相关阅读:
    js汉字转换为阿拉伯数字支持十到十九
    JS中判断是中文数字的函数
    一个JS正则的字符串替换函数
    thinkphp3.2.3使用formdata的多文件上传
    计算列表中的名字出现的订单中的订单总额
    配置ssl使用了不受支持的协议。 ERR_SSL_VERSION_OR_CIPHER_MISMATCH
    IIS8.5中的强制https直接修改web.config文件和顶级域名跳转www和过滤子目录不强制跳转
    关于wordpress4.8中的Twenty Seventeen主题的主题选项增加章节的实现
    excel中统计COUNTIFS的值为0
    thinkphp3.2.3集成phpexcel1.8导出设置单元格合并
  • 原文地址:https://www.cnblogs.com/wjlwo2ni/p/11975076.html
Copyright © 2011-2022 走看看