根据word模板(contract_templet.tld)生成并下载word合同及根据wordHTML模板(contract_templetHTML.tld)预览合同内容
1、action
String templete=ConstantsAppParams.CONTRACT_TEMPLET_DOC;//contract_templet.tld
String templeteHtml=ConstantsAppParams.CONTRACT_TEMPLET_HTML;//contract_templetHTML.tld
P2pAppInfo pai = (P2pAppInfo) baseLogic.getEntityByPK(P2pAppInfo.class,new Long(request.getParameter("priNumber")));
Map map = FreeMarkerUtils.convertBean(pai);//将实体类转成MAP
String flag = request.getParameter("flag");
if ("1".equals(flag)) {//预览
FreeMarkerUtils.createWordFile(request,map,templeteHtml,date + pai.getLoanName() + "借款合同.html",pai.getLoanContractNo());
StringBuffer path = new StringBuffer();
String str = request.getContextPath();
path.append(str);
path.append(File.separator);
path.append(ConstantsAppParams.CONTRACT__HTML);
path.append(File.separator);
path.append(pai.getLoanContractNo());
path.append(File.separator);
path.append(date + pai.getLoanName() + "借款合同.html");
response.setCharacterEncoding("utf-8");
PrintWriter writer = response.getWriter();
writer.write(path.toString());
writer.flush();
writer.close();
} else {//下载
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment;filename="
+ new String("借款合同".getBytes("gb2312"), "ISO8859-1")
+ ".doc");
// response.set
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
FreeMarkerUtils.downLoadWord(request, response,
map,
templete,
date + pai.getLoanName() + "借款合同");
}
2、FreeMarkerUtils
public class FreeMarkerUtils {
/**
* 生成预览的合同
* @param request
* @param response
* @param dataMap
* @param templateName
* @param fileName
* @throws Exception
*/
public static void downLoadWord(HttpServletRequest request,HttpServletResponse response,Map dataMap,String templateName,String fileName) throws Exception{
FreeMarkerConfigurationFactory fcf=new FreeMarkerConfigurationFactory();
Configuration configuration = fcf.createConfiguration();
fcf.setDefaultEncoding("utf-8");
configuration.setDefaultEncoding("utf-8");
String path=request.getSession().getServletContext().getRealPath("");
path=path+File.separator+ConstantsAppParams.CONTRACT_TEMPLET_FOLDER;
configuration.setDirectoryForTemplateLoading(new File(path));
Template template = configuration.getTemplate(templateName);
template.setEncoding("utf-8");
//PrintWriter out = response.getWriter();
OutputStreamWriter writer = new OutputStreamWriter( response.getOutputStream(),"utf-8");//下载
template.process(dataMap,writer);
writer.flush();
writer.close();
}
/**
* bean 转map
* @param bean
* @return
* @throws IntrospectionException
* @throws IllegalAccessException
* @throws InvocationTargetException
*/
public static Map convertBean(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
/**
* 生成可下载的合同
* @param request
* @param dataMap
* @param templateName
* @param string
* @param string2
* @throws Exception
*/
public static void createWordFile(HttpServletRequest request,Map dataMap,
String templateName, String string, String loanContractNo) throws Exception{
FreeMarkerConfigurationFactory fcf=new FreeMarkerConfigurationFactory();
Configuration configuration = fcf.createConfiguration();
fcf.setDefaultEncoding("utf-8");
configuration.setDefaultEncoding("utf-8");
String path=request.getSession().getServletContext().getRealPath("");
String filePath=path+File.separator+ConstantsAppParams.CONTRACT__HTML;
path=path+File.separator+ConstantsAppParams.CONTRACT_TEMPLET_FOLDER;
configuration.setDirectoryForTemplateLoading(new File(path));
Template template = configuration.getTemplate(templateName);
template.setEncoding("utf-8");
filePath=filePath+File.separator+loanContractNo;
File file=new File(filePath);
if(!file.exists()){
file.mkdir();
}
File file2=new File(filePath, string);
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file2),"UTF-8"));
template.process(dataMap,out);
out.flush();
out.close();
}
}
根据word模板(contract_templet.tld)生成并下载word合同及根据wordHTML模板(contract_templetHTML.tld)预览合同内容的更多相关文章
- 使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载
使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载 2018年06月07日 10:42:26 守望dfdfdf 阅读数:235 标签: jav ...
- T4模板根据DB生成实体类
1.前言 为什么会有这篇文章了,最近看到了一些框架,里面要写的代码太多了,故此就想偷懒,要是能写出一个T4模板,在数据库添加表后,根据模板就可以自动生成了类文件了,这样多好,心动不如行动.记得使用T4 ...
- POI生成Web版Word文件
POI生成Web版Word文件 1 通过URL的输入流实现 2 直接把Html文本写入到Word文件 所谓的使用POI生成Web版Word文件是指利用POI将Html代码插入到 ...
- IOS 预览word文档的集中方式
在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...
- IOS 预览pdf,word文档的集中方式
在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...
- java实现在线预览--poi实现word、excel、ppt转html
java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...
- word预览
word+excle表格在线浏览 word.ppt.xls文件实现在线预览的方式比较简单可以直接通过调用微软的在线预览功能实现 (预览前提:资源必须是公共可访问的) 通过iframe直接引用微软提供的 ...
- XPath2Doc,一个半自动采集网页生成Word Docx文件的工具,带企查查和天眼查模板
原始出处:https://www.cnblogs.com/Charltsing/p/XPath2Doc.html 很多人需要从网站采集一些数据填写Word模板,手工操作费时费力还容易出错,所以我给朋友 ...
- C#读取Word模板替换相应的字符串(标签)生成新的Word
在平常工作中,生成word的方式主要是C#读取html的模板文件处理之后保存为.doc文件,这样的好处是方便,快捷,能满足大部分的需求.不过有些特殊的需求并不能满足,如要生成的Word为一个表格,只是 ...
随机推荐
- 动态生成元素动作绑定,jquery 1.9如何实现
1.7后增加了 live()1.9后被移除了 网上说可以用 on() 代替 可以实际在动态生成元素上绑定动作,没效果,求解绝方法(用低版本的jQuery这种方法,求别说..) 答: 之前有老兄回答过类 ...
- 浅谈JavaScript eval() 函数
用js的人都应该知道eval()函数吧,虽然该函数用的极少,但它却功能强大,那么问题来了,为什么不常用呢?原因很简单,因为eval()函数是动态的执行其中的字符串,里面有可能是脚本,那么这样的话就有可 ...
- Java中封装、继承和多态
封装: 封装实际上使用方法将类的数据隐藏起来,控制用户对类的修改和访问数据的程度. 适当的封装可以让程式码更容易理解和维护,也加强了程式码的安全性. 访问修饰符有public,private,prot ...
- documentfragment
JS临时容器,父类是null,存储实际是把存储对象所有子类存储在里面,localStorage,浏览器支持情况下,保存本地变量
- 单例模式读取properties配置文件中的信息
public class ConfigManager { private static ConfigManager config = null; //创建Properties文件 读取配 ...
- ubuntukylin14安装ns-allinone-2.35教程(虚拟机ubuntu同理)
准备材料: 1.ubuntukylin14,百度进官网自行下载: 2.ns-allinone-2.35.tar.gz,百度进官网自行下载: 3.虚拟机:vmwareworkstation(可选). 4 ...
- DEX 方法超过64K限制和gradle编译OOM问题解决
如果你是一个android开发者,你至少听说过的Dalvik的蛋疼的64K方法限制.概括地说,在一个DEX文件,你可以调用很多的方法,但你只能调用它们最前面的65,536个 ,因为这是在方法调用集合中 ...
- 理解jquery的$.extend()、$.fn和$.fn.extend()
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); jQuery.fn jQuery.fn = jQuery.prototype ...
- haoop 断电后导致block文件损坏
hbase将dfs作为存储,公司测试环境断电后,hadoop集群会因此而损坏一些block文件,这个时候,客户端在读取文件时会报一些错: DataXceiver error processing RE ...
- Access 数据库连接 字符串
<!--Microsoft.Practices.EnterpriseLibrary.Data.dll 操作引用程序集--> <connectionStrings> <ad ...