使用freemarker生成html、doc文件【原创】_MySQL
程序员文章站
2022-05-10 13:33:30
...
bitsCN.com
10
36 37 38
语言: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 2 3 4 5异常交易预警信息 6 7 8 9
图斑编号: | ${tbbh!"无"} |
图斑面积: | ${tbmj!"无"} |
项目编号: | ${xmbh!"无"} |
座落单位名称: | ${zldwmc!"无"} |
地类名称: | ${dlmc!"无"} |
行政区代码: | ${xzqdm!"无"} |
转让方: | ${abdkSeller!"无"} |
买入方: | ${abdkBuyer!"无"} |
坡度: | ${abdkSlope!"无"} |
坡度方差: | ${abdkSlopevariance!"无"} |
交易前中心点x坐标: | ${abdkBx!"无"} |
交易前中心点y坐标: | ${abdkBy!"无"} |
交易后中心点x坐标: | ${abdkAx!"无"} |
交易后中心点y坐标: | ${abdkAy!"无"} |
交易前价格: | ${abdkBprice!"无"} |
交易后面积: | ${abdkAarea!"无"} |
交易后价格: | ${abdkAprice!"无"} |
处理意见: | ${abdkOpinion!"无"} |
是否处理: | ${abdkIssolved!"无"} |
风险等级: | ${abdkRisklevel!"无"} |
预警时间: | ${abdkWarningtime!"无"} |
是否异常: | ${abdkIsnormal!"无"} |
稻谷产量: | ${dgcl!"无"} |
稻谷均价: | ${dgjj!"无"} |
存储位置: | ${abdkFilepath!"无"} |
效果图:
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!"无"}
效果:
bitsCN.com上一篇: Nginx+PHP上传大文件设置
下一篇: Yii学习总结之数据访问对象 (DAO)