您好,欢迎访问一九零五行业门户网

使用freemarker生成html、doc文件【原创】_MySQL

bitscn.com
语言: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
10 图斑编号: ${tbbh!无}
11 图斑面积: ${tbmj!无}
12 项目编号: ${xmbh!无}
13 座落单位名称: ${zldwmc!无}
14 地类名称: ${dlmc!无}
15 行政区代码: ${xzqdm!无}
16 转让方: ${abdkseller!无}
17 买入方: ${abdkbuyer!无}
18 坡度: ${abdkslope!无}
19 坡度方差: ${abdkslopevariance!无}
20 交易前中心点x坐标: ${abdkbx!无}
21 交易前中心点y坐标: ${abdkby!无}
22 交易后中心点x坐标: ${abdkax!无}
23 交易后中心点y坐标: ${abdkay!无}
24 交易前价格: ${abdkbprice!无}
25 交易后面积: ${abdkaarea!无}
26 交易后价格: ${abdkaprice!无}
27 处理意见: ${abdkopinion!无}
28 是否处理: ${abdkissolved!无}
29 风险等级: ${abdkrisklevel!无}
30 预警时间: ${abdkwarningtime!无}
31 是否异常: ${abdkisnormal!无}
32 稻谷产量: ${dgcl!无}
33 稻谷均价: ${dgjj!无}
34 存储位置: ${abdkfilepath!无}
35
36 37 38 效果图:
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
其它类似信息

推荐信息