使用FreeMarker导出word文档(支持循环导出实时多张图片)

续上一期的更新内容  ,导出的是单张图片,直接在路径的src  里面填写对应的占位符,就可以了,随着需求的变化,那么今天我们继续往下写一个循环导出多张图片到word里面。

使用FreeMarker导出word文档(支持导出单张图片)

首先我们需要进行在word 里面把对应的模版设置好。

使用FreeMarker导出word文档(支持循环导出实时多张图片)_第1张图片

设置好点击另存为,目前我用的html 格式的,网上也有很多xml  形式的,每种写法还不一样

使用FreeMarker导出word文档(支持循环导出实时多张图片)_第2张图片

接着我们用记事本进行打开,第一步一定要先修改编码格式为utf-8的

使用FreeMarker导出word文档(支持循环导出实时多张图片)_第3张图片

然后我们将html 中head中的标签全部,如果编码不修改,head标签不删除,将来导出的时候就是乱码,这个坑是很深的,排查了好长时间才解决。

使用FreeMarker导出word文档(支持循环导出实时多张图片)_第4张图片

这些都修改好后,我们点击另存为将这个模版改成后缀为ftl形式的

接下来我们就开始编写代码

	@Override
	public void exporttdYsdWord(HttpServletRequest request,
			HttpServletResponse response, String objId, String gllx) {
		List> list = null;
		List> list3 = null;
		List> list5 = null;
		String ysdName = "";
		String ysdSql = "";
		String zpSql = "";
		String sdsgfa = "";
		String ysgjjd = "";
		String sdsgfaEname = "";
		if ("0".equals(gllx)) {
			sdsgfa = "sdsgfa";
			ysgjjd = "ysgjjd";
			sdsgfaEname = "sczbjys_sczbjys_sdsgfa";
			ysdName = "中间环节验收报告";
			ysdSql = "select t.gcmc,t.jgdw,t.jgdwfzr,t.jgdwfzrdh,"
						+ "t.tdsgdw,t.tdsgdwlxr,t.tdsgdwlxrdh,to_char(t.sqyssj,'yyyy-MM-dd') sqyssj,"
						+ "t.czwt,  (select a.username from gw_yhgl_user a where a.id = t.yxbzrm) yxbzrm, (select h.value  from gw_limit_enums h  where h.enumName like '%sczbjys_sczbjys_yxbz%' and h.key = t.yxbz) yxbz,(select t.value from   gw_limit_enums t   where   t.enumName like '%sczbjys_sczbjys_ysgjjd%'  and t.key=ysgjjd)  ysgjjd from GW_SCZBJYS_ZLC t where t.OBJ_ID = ?";
		}
		list = this.hibernateDao
				.queryForListWithSql(ysdSql,new String[]{objId});

		Map map0 = new HashMap();
		Map map = new HashMap();
        StringBuilder sb = new StringBuilder();
        String aa="";
		zpSql="select   t.ipath ,t.dpt from   gw_pig_address_rw t   where  t.taskid=?   and t.ipath is not   null  and t.dpt='问题照片'  and t.dpt  is not null   and t.ipath   not  like '%dat%'" ;
				list3 = this.hibernateDao
				.queryForListWithSql(zpSql,new String[]{objId});
				
			String 	zpSqla="select   t.ipath ,t.dpt from   gw_pig_address_rw t   where  t.taskid=?   and t.ipath is not   null   and t.dpt  is not null   and t.ipath   not  like '%dat%'" ;
				list5 = this.hibernateDao
				.queryForListWithSql(zpSqla,new String[]{objId});
				if(list3.size()>0){
					
		            for (Map stringObjectMap : list3) {
		                if (stringObjectMap.get("DPT").equals("问题照片")) {
//		                	map.put("PSZP", "" + PlatformConfigUtil.getString("TASK_PIC_ADDRESS") + stringObjectMap.get("IPATH"));
//		                     aa= String.valueOf(sb.append(PlatformConfigUtil.getString("TASK_PIC_ADDRESS")+ stringObjectMap.get("IPATH")).append(","));
//		                	 aa= String.valueOf(sb.append("  ").append(","));

		                    aa= String.valueOf(sb.append("").append(","));

		                	

		                }
		            }
				}else{
					
		            for (Map stringObjectMap : list5) {
		                if (!stringObjectMap.get("DPT").equals("问题照片")) {
//		                	map.put("PSZP", "" + PlatformConfigUtil.getString("TASK_PIC_ADDRESS") + stringObjectMap.get("IPATH"));
//		                     aa= String.valueOf(sb.append(PlatformConfigUtil.getString("TASK_PIC_ADDRESS")+ stringObjectMap.get("IPATH")).append(","));
		        			String   hh="").append(","));


		                }
		            }
					
				}
				
				if(aa.length()>0){
				      String a1 = aa.substring(0, aa.length() - 1);
				      map.put("PSZP", a1);
				}else{

					if(aa.length()>0){
						  String a1 = aa.substring(0, aa.length() - 1);
					      map.put("PSZP", a1);
					}else{
					      map.put("PSZP", "");
					}
				  
				}


		if (!Tool.isEmptyList(list)) {
			map0 = list.get(0);
			if (Tool.isEmptyStr(map0.get("gcmc"))) {
				map.put("GCMC", "");
			} else {
				map.put("GCMC", map0.get("gcmc"));
			}
			if (Tool.isEmptyStr(map0.get("jgdw"))) {
				map.put("JGDW", "");
			} else {
				map.put("JGDW",
						getEnumsValueFromEnumDB("sczbjys_sczbjys_jgdw",map0.get("jgdw")));
			}

			if (Tool.isEmptyStr(map0.get("tdsgdw"))) {
				map.put("TDSGDW", "");
			} else {
				map.put("TDSGDW", map0.get("tdsgdw"));
			}
			if (Tool.isEmptyStr(map0.get("tdsgdwlxr"))) {
				map.put("TDSGDWLXR", "");
			} else {
				map.put("TDSGDWLXR", map0.get("tdsgdwlxr"));
			}
			if (Tool.isEmptyStr(map0.get("tdsgdwlxrdh"))) {
				map.put("TDSGDWLXRDH", "");
			} else {
				map.put("TDSGDWLXRDH", map0.get("tdsgdwlxrdh"));
			}
			if (Tool.isEmptyStr(map0.get("yxbz"))) {
				map.put("YXBZ", "");
			} else {
				map.put("YXBZ", map0.get("yxbz"));
			}
			if (Tool.isEmptyStr(map0.get("yxbzrm"))) {
				map.put("YXBZRM", "");
			} else {
				map.put("YXBZRM", map0.get("yxbzrm"));
			}
			
			if (Tool.isEmptyStr(map0.get("ysgjjd"))) {
				map.put("YSGJJD", "");
			} else {
				map.put("YSGJJD", map0.get("ysgjjd"));
			}
			if (Tool.isEmptyStr(map0.get("sqyssj"))) {
				map.put("SQYSSJ", "");
			} else {
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
				String string = map0.get("sqyssj"); 
				Date parse = null;
				try {
					parse = simpleDateFormat.parse(string);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			String format = simpleDateFormat.format(parse);
	        StringBuilder myName = new StringBuilder(format);
	        myName.setCharAt(4, '年');
	        myName.setCharAt(7, '月');
	        myName.append("日");

	        System.out.println(myName);
//  时间变成 年月日形式
				map.put("SQYSSJ", myName.toString());
			}
			if (Tool.isEmptyStr(map0.get("czwt"))) {
				map.put("CZWT", "");
			} else {
				map.put("CZWT", map0.get("czwt"));
			}
			map.put("SJDW", "设计单位");
			map.put("JLDW", "监理");
			map.put("BD", "121");
			map.put("SDJG", "结构好");
			map.put("CD", "长度1");
			map.put("JCJ", "井口1");
			
			String   hh="

 

图片的格式高宽度提前在代码里面设置好,然后我们在模版里面用一个占位符来进行替代就可以了。

 设置下载格式

	public static void exportWorde(HttpServletRequest request,
			HttpServletResponse response, Map map,
			String templateName, String fileName)  throws IOException {
		String pathString = request.getSession().getServletContext().getRealPath("/WEB-INF/templete/");
    	logger.info("获取到的模板路径是:templetePath------->" + pathString);
    	configuration.setDirectoryForTemplateLoading(new File(pathString));
    	
    	Template freemarkerTemplate = configuration.getTemplate(templateName);
        File file = null;
        InputStream fin = null;
        ServletOutputStream out = null;
        try {
            // 调用工具类的createDoc方法生成Word文档
            file = createDoce(map,freemarkerTemplate);
            fin = new FileInputStream(file);
 
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/msword");
            // 设置浏览器以下载的方式处理该文件名
            fileName = fileName + DateUtil.currentDateToString() + ".doc";
            response.setHeader("Content-Disposition", "attachment;filename="
                    .concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));
 
            out = response.getOutputStream();
            byte[] buffer = new byte[512];  // 缓冲区
            int bytesToRead = -1;
            // 通过循环将读入的Word文件的内容输出到浏览器中
            while((bytesToRead = fin.read(buffer)) != -1) {
                out.write(buffer, 0, bytesToRead);
            }
        } finally {
            if(fin != null) fin.close();
            if(out != null) out.close();
            if(file != null) file.delete(); // 删除临时文件
        }
		
	}

设置编码:

    private static File createDoce(Map dataMap, Template template) {
        String name =  ".doc";
        File f = new File(name);
        Template t = template;
        try {
            // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开
            Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");
            t.process(dataMap, w);
            w.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        return f;
    }

 导出效果

使用FreeMarker导出word文档(支持循环导出实时多张图片)_第5张图片

若本文对你有所帮助,请一键三连,说不定后期工作中就用到了。

你可能感兴趣的:(word,eclipse,spring,java)