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)预览合同内容的更多相关文章

  1. 使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载

    使用java的 htpUrlConnection post请求 下载pdf文件,然后输出到页面进行预览和下载 2018年06月07日 10:42:26 守望dfdfdf 阅读数:235 标签: jav ...

  2. T4模板根据DB生成实体类

    1.前言 为什么会有这篇文章了,最近看到了一些框架,里面要写的代码太多了,故此就想偷懒,要是能写出一个T4模板,在数据库添加表后,根据模板就可以自动生成了类文件了,这样多好,心动不如行动.记得使用T4 ...

  3. POI生成Web版Word文件

    POI生成Web版Word文件 1       通过URL的输入流实现 2       直接把Html文本写入到Word文件 所谓的使用POI生成Web版Word文件是指利用POI将Html代码插入到 ...

  4. IOS 预览word文档的集中方式

    在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...

  5. IOS 预览pdf,word文档的集中方式

    在iPhone中可以很方便的预览文档文件,如:pdf.word等等,这篇文章将以PDF为例.介绍三种预览PDF的方式,又分别从本地pdf文档和网络上的pdf文档进行对比. 预览本地PDF文档: 1.使 ...

  6. java实现在线预览--poi实现word、excel、ppt转html

    java实现在线预览 - -之poi实现word.excel.ppt转html 简介 java实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服 ...

  7. word预览

    word+excle表格在线浏览 word.ppt.xls文件实现在线预览的方式比较简单可以直接通过调用微软的在线预览功能实现 (预览前提:资源必须是公共可访问的) 通过iframe直接引用微软提供的 ...

  8. XPath2Doc,一个半自动采集网页生成Word Docx文件的工具,带企查查和天眼查模板

    原始出处:https://www.cnblogs.com/Charltsing/p/XPath2Doc.html 很多人需要从网站采集一些数据填写Word模板,手工操作费时费力还容易出错,所以我给朋友 ...

  9. C#读取Word模板替换相应的字符串(标签)生成新的Word

    在平常工作中,生成word的方式主要是C#读取html的模板文件处理之后保存为.doc文件,这样的好处是方便,快捷,能满足大部分的需求.不过有些特殊的需求并不能满足,如要生成的Word为一个表格,只是 ...

随机推荐

  1. 个人项目(JUnit单元测试)

    ---恢复内容开始--- 一.             题目简介 这次的单元测试我选择作了一个基本运算的程序,该程序实现了加,减,乘,除,平方,倒数的运算,该程序进行测试比较的简单,对于初步接触JUn ...

  2. jsp按钮隐藏自动点击

    <%@ page language="java" import="java.util.*" pageEncoding="big5"%& ...

  3. jQuery中$.fn的用法示例介绍

    $.fn是指jquery的命名空间,加上fn上的方法及属性,会对jquery实例每一个有效,下面有个不错的示例,喜欢的朋友可以参考下 如扩展$.fn.abc(),即$.fn.abc()是对jquery ...

  4. jquery 插件

    1 jquery.slimscroll.min.js:虚拟滚轴 2 jquery.steps.js:步骤(注册下一步) 3 jquery-barcode.js:条形码

  5. 手机web——自适应网页设计(html/css控制)

    一. 允许网页宽度自动调整: "自适应网页设计"到底是怎么做到的? 其实并不难. 首先,在网页代码的头部,加入一行viewport元标签. <meta name=" ...

  6. unity, 只发射一个粒子的粒子系统

  7. Ubuntu14.04用apt在线/离线安装CDH5.1.2[Apache Hadoop 2.3.0]-old

    用markdown重写,请稳步这里http://www.cnblogs.com/lion.net/p/5477899.html

  8. Mysql大量数据快速排序方案

    日常开发中经常需要对数据进行排序,通常可以讲数据库中的数据获取到后通过程序在内存中进行排序,但是这样排序需要将排序内容从数据库中查询到内容,同时使用程序算法进行排序,然后将排序结果更新入数据库,这样排 ...

  9. (转)ViewPager,ScrollView 嵌套ViewPager滑动冲突解决

    ViewPager,ScrollView 嵌套ViewPager滑动冲突解决 本篇主要讲解一下几个问题 粗略地介绍一下View的事件分发机制 解决事件滑动冲突的思路及方法 ScrollView 里面嵌 ...

  10. .NET操作Xml类

    using System; using System.Collections.Generic; using System.Text; using System.Data; using System.I ...