语言:java
功能:使用freemarker生成html、doc
1、生成html
1 public void updateuseFreemarker2html(String abdkId) { 2 // 要填入模本的数据文件 3 Session session = sessionFactory.getCurrentSession(); 4 String hql = "from AbnmDktranssupervision as abnmDktranssupervision "; 5 AbnmDktranssupervision result = null; 6 try { 7 // 根据id用hql语言获取一个对象的数据 8 result = (AbnmDktranssupervision) session.get( 9 AbnmDktranssupervision.class, Integer.parseInt(abdkId)); 10 } catch (Exception e) { 11 e.printStackTrace(); 12 } 13 14 15 16 // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载, 17 // 这里我们的模板是放在cn.edu.bnu.land.common包下面 18 configuration = new Configuration(); 19 configuration.setEncoding(Locale.CHINA, "utf-8"); 21 configuration.setClassForTemplateLoading(this.getClass(), 22 "/cn/edu/bnu/land/common"); 23 Template t = null; 24 25 try { 26 // freemarker2html.ftl为要装载的html模板 27 t = configuration.getTemplate("freemarker2html.ftl"); 28 t.setEncoding("utf-8"); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 33 34 // 输出文档路径及名称 35 Calendar ca = Calendar.getInstance(); 36 Date date = ca.getTime(); 37 java.text.DateFormat format2 = new java.text.SimpleDateFormat( 38 "yyyyMMddhhmmss"); 39 String dateString = format2.format(date); 40 //文件名称 41 String m_fileName = result.getTbbh() + "_" + dateString + ".html"; 42 // 43 String realPath=this.getClass().getClassLoader().getResource("/").getPath(); 44 System.out.println(realPath); 45 int pos=realPath.indexOf("/WEB-INF"); 46 System.out.println(pos); 47 realPath=realPath.substring(0, pos); 48 realPath=realPath+"/Upload/abnmTransSupHtml/"; 49 System.out.println(realPath); 50 String filePath = realPath +m_fileName; 51 result.setAbdkFilepath("/tdlzJGXT/Upload/abnmTransSupHtml/"+m_fileName); 53 54 55 File outFile = new File(filePath); 56 Writer out = null; 57 try { 58 out = new BufferedWriter(new OutputStreamWriter( 59 new FileOutputStream(outFile), "utf-8")); 60 } catch (Exception e1) { 61 e1.printStackTrace(); 62 } 63 64 try { 65 t.process(result, out); 66 out.close(); 67 } catch (TemplateException e) { 68 e.printStackTrace(); 69 } catch (IOException e) { 70 e.printStackTrace(); 71 } 72 } 73 74 }
freemarker3html.ftl
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 5 <title>异常交易预警信息</title> 6 </head> 7 8 <body> 9 <table> 10 <tr><td>图斑编号:</td><td>${tbbh!"无"}</td></tr> 11 <tr><td>图斑面积:</td><td>${tbmj!"无"}</td></tr> 12 <tr><td>项目编号:</td><td>${xmbh!"无"}</td></tr> 13 <tr><td>座落单位名称:</td><td>${zldwmc!"无"}</td></tr> 14 <tr><td>地类名称:</td><td>${dlmc!"无"}</td></tr> 15 <tr><td>行政区代码:</td><td>${xzqdm!"无"}</td></tr> 16 <tr><td>转让方:</td><td>${abdkSeller!"无"}</td></tr> 17 <tr><td>买入方:</td><td>${abdkBuyer!"无"}</td></tr> 18 <tr><td>坡度:</td><td>${abdkSlope!"无"}</td></tr> 19 <tr><td>坡度方差:</td><td>${abdkSlopevariance!"无"}</td></tr> 20 <tr><td>交易前中心点x坐标:</td><td>${abdkBx!"无"}</td></tr> 21 <tr><td>交易前中心点y坐标:</td><td>${abdkBy!"无"}</td></tr> 22 <tr><td>交易后中心点x坐标:</td><td>${abdkAx!"无"}</td></tr> 23 <tr><td>交易后中心点y坐标:</td><td>${abdkAy!"无"}</td></tr> 24 <tr><td>交易前价格:</td><td>${abdkBprice!"无"}</td></tr> 25 <tr><td>交易后面积:</td><td>${abdkAarea!"无"}</td></tr> 26 <tr><td>交易后价格:</td><td>${abdkAprice!"无"}</td></tr> 27 <tr><td>处理意见:</td><td>${abdkOpinion!"无"}</td></tr> 28 <tr><td>是否处理:</td><td>${abdkIssolved!"无"}</td></tr> 29 <tr><td>风险等级:</td><td>${abdkRisklevel!"无"}</td></tr> 30 <tr><td>预警时间:</td><td>${abdkWarningtime!"无"}</td></tr> 31 <tr><td>是否异常:</td><td>${abdkIsnormal!"无"}</td></tr> 32 <tr><td>稻谷产量:</td><td>${dgcl!"无"}</td></tr> 33 <tr><td>稻谷均价:</td><td>${dgjj!"无"}</td></tr> 34 <tr><td>存储位置:</td><td>${abdkFilepath!"无"}</td></tr> 35 </table> 36 37 </body> 38 </html>
效果图:
2、生成doc
1 // ues freemarker+xml to doc 2 public void updateuseFreemarker2doc(String abdkId) { 3 // 要填入模本的数据文件 4 Session session = sessionFactory.getCurrentSession(); 5 String hql = "from AbnmDktranssupervision as abnmDktranssupervision "; 6 AbnmDktranssupervision result = null; 7 try { 8 // System.out.println(hql); 9 result = (AbnmDktranssupervision) session.get( 10 AbnmDktranssupervision.class, Integer.parseInt(abdkId)); 11 } catch (Exception e) { 12 e.printStackTrace(); 13 } 14 15 16 17 // 设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载, 18 // 这里我们的模板是放在com.havenliu.document.template包下面 19 configuration = new Configuration(); 20 configuration.setDefaultEncoding("utf-8"); 21 configuration.setClassForTemplateLoading(this.getClass(), 22 "/cn/edu/bnu/land/common"); 23 Template t = null; 24 25 try { 26 // test.ftl为要装载的模板 27 t = configuration.getTemplate("freemarker2doc.ftl"); 28 t.setEncoding("utf-8"); 29 } catch (IOException e) { 30 e.printStackTrace(); 31 } 32 33 34 // 输出文档路径及名称 35 Calendar ca = Calendar.getInstance(); 36 Date date = ca.getTime(); 37 java.text.DateFormat format2 = new java.text.SimpleDateFormat( 38 "yyyyMMddhhmmss"); 39 String dateString = format2.format(date); 40 41 //文件名称 42 String m_fileName = result.getTbbh() + "_" + dateString + ".doc"; 43 // 44 String realPath=this.getClass().getClassLoader().getResource("/").getPath(); 45 //System.out.println(realPath); 46 int pos=realPath.indexOf("/WEB-INF"); 47 //System.out.println(pos); 48 realPath=realPath.substring(0, pos); 49 realPath=realPath+"/Upload/abnmTransSupDoc/"; 50 //System.out.println(realPath); 51 String filePath = realPath +m_fileName; 52 result.setAbdkFilepath("/tdlzJGXT/Upload/abnmTransSupDoc/"+m_fileName); 53 54 55 56 57 58 File outFile = new File(filePath); 59 Writer out = null; 60 try { 61 out = new BufferedWriter(new OutputStreamWriter( 62 new FileOutputStream(outFile), "utf-8")); 63 } catch (Exception e1) { 64 e1.printStackTrace(); 65 } 66 67 try { 68 t.process(result, out); 69 out.close(); 70 } catch (TemplateException e) { 71 e.printStackTrace(); 72 } catch (IOException e) { 73 e.printStackTrace(); 74 } 75 76 }
freemarker2doc.ftl
1 图斑编号:${tbbh!"无"} 2 图斑面积:${tbmj!"无"} 3 项目编号:${xmbh!"无"} 4 座落单位名称:${zldwmc!"无"} 5 地类名称:${dlmc!"无"} 6 行政区代码:${xzqdm!"无"} 7 转让方:${abdkSeller!"无"} 8 买入方:${abdkBuyer!"无"} 9 坡度:${abdkSlope!"无"} 10 坡度方差:${abdkSlopevariance!"无"} 11 交易前中心点x坐标:${abdkBx!"无"} 12 交易前中心点y坐标:${abdkBy!"无"} 13 交易后中心点x坐标:${abdkAx!"无"} 14 交易后中心点y坐标:${abdkAy!"无"} 15 交易前价格:${abdkBprice!"无"} 16 交易后面积:${abdkAarea!"无"} 17 交易后价格:${abdkAprice!"无"} 18 处理意见:${abdkOpinion!"无"} 19 是否处理:${abdkIssolved!"无"} 20 风险等级:${abdkRisklevel!"无"} 21 预警时间:${abdkWarningtime!"无"} 22 是否异常:${abdkIsnormal!"无"} 23 稻谷产量:${dgcl!"无"} 24 稻谷均价:${dgjj!"无"} 25 存储位置:${abdkFilepath!"无"}
效果: