語言:javahtml
功能:使用freemarker生成html、docjava
一、生成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.ftlsession
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>
效果圖:ui
二、生成docthis
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.ftlspa
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!"無"}
效果:code