利用freemarker生成包含合并单元格的表格的word文档

1.项目中有需求要生成那种合并单元格而且行数不确定的情况,之前有用过freemarker生成固定格式的word文档,网上找了很多资料,经过两天的研究,现在提供一个东西可以供大家参考。

需求图:


2.然后把这个word文档另存为xml格式的,这个可以参照我之前写的一篇文章:点击打开链接

3.然后利用xml代码格式化工具(网上有很多),把你的xml代码全部格式化。然后另外保存一个xml文档,最好是在eclipse中生成,因为改起来很快。在新的xml文档中,查找标签如下:<w:tbl>代表表格的开始,<w:tr>代表这个表格的行,只要知道这两个标签,就可以根据需求实现我们的循环了。

4.循环的写法和jsp页面的迭代标签比较相似


结尾也是</#list>

写好之后把这个xml修改为ftl格式的文件

5.然后在代码里面把生成的集合放在这里进行显示

public void exportRegisterModel(){
		try {  
			SureDocument sureDocument = sureDocumentService.getById(model.getId());
			Configuration configuration = new Configuration();  
	        configuration.setDefaultEncoding("utf-8");  
	        
	        
	        Map<String,Object> dataMap=new HashMap<String,Object>();  
	        RegisterCover registerCover = sureDocument.getRegisterCover();
	        //封面
	        dataMap.put("name", registerCover.getName());
	        dataMap.put("version", registerCover.getVersion());
	        dataMap.put("jidi", registerCover.getJidi());
	        dataMap.put("year", registerCover.getYear());
	        dataMap.put("month", registerCover.getMonth());
	        dataMap.put("name2", registerCover.getName2());
	        //课程表
	        HqlHelper hqlHelper = new HqlHelper(Schedule.class, "s")
	        		.addWhereCondition("s.sureDocument.id = ?",sureDocument.getId())
	        		.addOrderByProperty("s.dateTime", true);
	        List<Schedule> scheduleList = scheduleService.queryWithSplitPage(hqlHelper, currentPage, pageSize);
	        List<ScheduleForm> scheduleFormList = scheduleService.coverPO2VO(scheduleList);
	        dataMap.put("scheduleForms", scheduleFormList);
	        dataMap.put("position", registerCover.getPosition());
	        //作息时间

	        //学员名单
			hqlHelper = new HqlHelper(TrainClass.class, "t")
	        		.addWhereCondition("t.sureDocument.id = ?",sureDocument.getId())
	        		.addOrderByProperty("t.createdTime", true);
			List<TrainClass> trainClassList = trainClassService.queryWithSplitPage(hqlHelper, null, null);
			//装班级的集合
			List<TrainClass2Register> trainClass2Registers = new ArrayList<TrainClass2Register>();
			if(trainClassList!=null&&trainClassList.size()!=0){
				for(TrainClass trainClass : trainClassList){
					TrainClass2Register trainClass2Register = new TrainClass2Register();
					trainClass2Register.setPcname(trainClass.getName());
					//装学员的集合
					List<Person2Register> person2Registers = new ArrayList<Person2Register>();
					
					HqlHelper hqlHelper2 = new HqlHelper(TrainClassAndUser.class, "t")
							.addWhereCondition("t.trainClass.id = ?", trainClass.getId());
					
					List<TrainClassAndUser> trainClassAndUserList = trainClassAndUserService.queryWithSplitPage(hqlHelper2, null, null);
					String banzhang = "";
					String shuji = "";
					String zuzhang = "";
					String pnum = "";
					if(trainClassAndUserList!=null&&trainClassAndUserList.size()!=0){
						pnum = trainClassAndUserList.size()+"";
						for(TrainClassAndUser classAndUser : trainClassAndUserList){
							if(classAndUser.getType().equals("1")){//判断是否为班长
								banzhang += "   "+classAndUser.getUser().getName();
							}else if(classAndUser.getType().equals("3")){//判断是否为书记
								shuji += "   "+classAndUser.getUser().getName(); 
							}else if(classAndUser.getType().equals("5")){//判断是否为组长
								zuzhang += "   "+classAndUser.getUser().getName(); 
							}
							
							String hql = "From Personal p where p.user.id = " + classAndUser.getUser().getId();
							Personal personal = personalService.querySingleWithHql(hql);
							if(personal!=null){
								Person2Register person2Register = new Person2Register();
								person2Register.setPname(personal.getName());
								person2Register.setPnational(personal.getNational());
								person2Register.setPsex(personal.getSex());
								person2Register.setProom(classAndUser.getStayName());
								String parea = "";
								//所在机构
								if(personal.getProvince()!=null&&personal.getProvince().length()!=0){
									Department department = departmentService.getById(Long.parseLong(personal.getProvince()));
									parea = department.getName();
								}
								if(personal.getCity()!=null&&personal.getCity().length()!=0){
									Department department = departmentService.getById(Long.parseLong(personal.getCity()));
									parea = department.getParent().getName()+department.getName();
								}
								if(personal.getArea()!=null&&personal.getArea().length()!=0){
									Department department = departmentService.getById(Long.parseLong(personal.getArea()));
									parea = department.getParent().getParent().getName() + department.getParent().getName()+department.getName();
								}
								person2Register.setParea(parea);
								person2Register.setPposition(personal.getPosition());
								person2Register.setPphone(personal.getPhone());
								
								person2Registers.add(person2Register);
							}else{
								Person2Register person2Register = new Person2Register();
								User user = classAndUser.getUser();
								person2Register.setPname(user.getName());
								person2Register.setPnational("");
								person2Register.setPsex(user.getGender());
								person2Register.setProom(classAndUser.getStayName());
								String parea = "";
								//所在机构
								Department department = user.getDepartment();
								if(department.getType().equals("1")){
									parea = department.getName(); 
								}
								if(department.getType().equals("2")){
									parea = department.getParent().getName()+department.getName();
								}
								if(department.getType().equals("3")){
									parea = department.getParent().getParent().getName() + department.getParent().getName()+department.getName();
								}
								person2Register.setParea(parea);
								person2Register.setPposition("");
								person2Register.setPphone(user.getPhoneNumber());
								
								person2Registers.add(person2Register);
							}
						}
					}
					trainClass2Register.setPerson2Registers(person2Registers);
					trainClass2Register.setBanzhang(banzhang);
					trainClass2Register.setShuji(shuji);
					trainClass2Register.setZuzhang(zuzhang);
					trainClass2Register.setPnum(pnum);
					trainClass2Registers.add(trainClass2Register);
				}
			}
	        dataMap.put("xueyuanmingdan", "");
	        dataMap.put("trainClass2Registers", trainClass2Registers);

	        configuration.setClassForTemplateLoading(this.getClass(), "/cn/mym/sysi/template");  //FTL文件所存在的位置  
	        Template t = null;  
	        t = configuration.getTemplate("registerModel.ftl"); //文件名
	        t.setEncoding("utf-8");  
	        
	        String fileName = sureDocument.getName() + "报名手册.doc";  
	          
	        //ActionContext ctx = ActionContext.getContext(); 
	      //定义输出类型
	        HttpServletResponse response = getResponse();
     		response.reset();
     		response.setHeader("Content-type", "application/msword;charset=ISO8859-1");  
     		response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
     		response.setHeader("Content-disposition", "attachment; filename="+new String(fileName.getBytes(),"ISO8859-1"));
            response.setCharacterEncoding("utf-8");  
            PrintWriter out = response.getWriter();  
            t.process(dataMap, out);  
            out.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
	}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gaoshili001/article/details/52357383
个人分类: freemarker
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

利用freemarker生成包含合并单元格的表格的word文档

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭