import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.util.Map;

import freemarker.template.Configuration;

import freemarker.template.Template;

public class DocumentHandler {

private Configuration configuration = null;

/**

* 创建configuration

* @param ftlSrc  模版存放路径,ftl所在的文件夹路径

*/

public DocumentHandler(String ftlSrc) {

configuration = new Configuration();

configuration.setDefaultEncoding("utf-8");

try {

configuration.setDirectoryForTemplateLoading(new File(ftlSrc));

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 根据ftl生成word方法

* @param dataMap   -- 数据集

* @param fileSrc   -- 生成后的文件地址

* @param ftlName   -- ftl模版名称

*/

public void createOrderAndDown(Map<String,Object> dataMap,String fileSrc,String ftlName) {

Template t=null;

try {

//test.ftl为要装载的模板

t = configuration.getTemplate(ftlName);

} catch (IOException e) {

e.printStackTrace();

}

//输出文档路径及名称

File outFile = new File(fileSrc);

//如果输出目标文件夹不存在,则创建

if (!outFile.getParentFile().exists()){

outFile.getParentFile().mkdirs();

}

Writer out = null;

try {

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));

//生成

t.process(dataMap, out);

} catch (Exception e) {

e.printStackTrace();

}finally {

if(out!=null){

//关闭流

try {

out.flush();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

/**

* 要把模版生成合同

* @param dataMap   数据map集合

* @param fileSrc   生成后的文件路径

*/

public void createDoc(Map<String,Object> dataMap,String fileSrc) {

//设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载,

//这里我们的模板是放在/com/zhiwei/credit/util/xmlToWord/firstCreditor包下面

//      configuration.setClassForTemplateLoading(this.getClass(), "/com/zhiwei/credit/util/xmlToWord/firstCreditor");

//      configuration.setServletContextForTemplateLoading(ServletActionContext.getContext(), "WEB-INF/templates");

//      configuration.setDirectoryForTemplateLoading(arg0);

Template t=null;

try {

//test.ftl为要装载的模板

t = configuration.getTemplate("firstCreditor.ftl");

} catch (IOException e) {

e.printStackTrace();

}

//输出文档路径及名称

File outFile = new File(fileSrc);

//如果输出目标文件夹不存在,则创建

if (!outFile.getParentFile().exists()){

outFile.getParentFile().mkdirs();

}

Writer out = null;

try {

out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));

//生成

t.process(dataMap, out);

} catch (Exception e) {

e.printStackTrace();

}finally {

if(out!=null){

//关闭流

try {

out.flush();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

DocumentHandler-ftl生成word的更多相关文章

  1. JAVAWEB使用FreeMarker利用ftl把含有图片的word模板生成word文档,然后打包成压缩包进行下载

    这是写的另一个导出word方法:https://www.cnblogs.com/pxblog/p/13072711.html 引入jar包,freemarker.jar.apache-ant-zip- ...

  2. FreemarkerJavaDemo【Android将表单数据生成Word文档的方案之一(基于freemarker2.3.28,只能java生成)】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 这个方案只能在java中运行,无法在Android项目中运行.所以此方案是:APP将表单数据发送给后台,后台通过freemarker ...

  3. JAVA生成word的几种方法对比

    首先介绍几种java导出word方案 1.Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过JNI的方式实现 ...

  4. 使用freemarker生成word,步骤详解并奉上源代码

    1.   步骤 1.    用word编辑好模板 1. 普通字符串替换为 ${string} 2. 表格循环用标签 <#list userList as user> 姓名:${user.u ...

  5. java freemark生成word文档

    1.下载freemarker-2.3.19.jar 2.把要填充的内容用  ${title},${no}代替 3.用word 打开,保存为2003xml 4.打开生成xml文件,看下有没有把表达式  ...

  6. 使用freemarker模板生成word文档

    项目中最近用到这个东西,做下记录. 如下图,先准备好一个(office2003)word文档当做模板.文档中图片.姓名.性别和生日已经使用占位符代替,生成过程中将会根据实际情况进行替换. 然后将wor ...

  7. 【java】Freemarker 动态生成word(带图片表格)

    1.添加freemarker.jar 到java项目. 2.新建word文档. 3.将文档另存为xml 格式. 4.将xml格式化后打开编辑(最好用notepad,有格式),找到需要替换的内容,将内容 ...

  8. springboot中使用freemarker生成word文档并打包成zip下载(简历)

    一.设计出的简历模板图以及给的简历小图标切图         二.按照简历模板图新建简历word文件 :${字段名},同时将图片插入到word中,并将建好的word文件另存为xml文件:    三.直 ...

  9. freemarker根据模板生成word文件实现导出功能

    一.准备工作 1.创建一个03的word文档,动态的数据用占位符标志占位(如testname).然后另存为word2003的xml文件. 2.格式化xml文件,占位符的位置用${testname}代替 ...

随机推荐

  1. 实例讲解ip地址、子网掩码、可用地址范围的计算

    关于ip以及相关的掩码.网络号等概念可以查看相关的博客.资料,这些东西很容易找着,一搜一大片.本文主要记录通过实例进行ip相关的计算. 我自己使用的在线网络计算器地址:https://www.sojs ...

  2. CentOS7.0 内核(3.10.0-123.el7.x86_64)bug导致KVM物理机重启

    一.问题描述 服务器硬件:DELL R720 系统版本:CentOS7.0 内核版本:3.10.0-123.el7.x86_64 故障现象:偶尔会重启 二.问题原因 经查看dmesg日志发现是kern ...

  3. selenium+python自动化100-centos上搭建selenium启动chrome浏览器headless无界面模式

    环境准备 前言 selenium在windows机器上运行,每次会启动界面,运行很不稳定.于是想到用chrome来了的headless无界面模式,确实方便了不少. 为了提高自动化运行的效率和稳定性,于 ...

  4. P1092 虫食算[搜索]

    这个式子是是由\(A\sim A+N\)组成的,那么\(A\sim A+N\)就只能等于\(0\sim N-1\),因此我们每次对\(A\sim A+N\)的取值做一个新的排列,然后judge一下当前 ...

  5. “2018宁夏邀请赛 ” 兼 “The 2019 Asia Yinchuan First Round Online Programming”

    ------------7题弟弟,被各位半小时13题的大佬打惨了(滑稽)---------- 签到题就不写了. F :Moving On            (1247ms) 题意:给定大小为N的带 ...

  6. appium报错:An unknown server-side error occurred while processing the command. Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET

    Appium Desktop版本:1.9.0 xcode版本:9.4.1 测试机:iPhone7  11.3系统 问题描述:在xcode上的produc的text运行是可以将WebDriverAgen ...

  7. ECMAScript规范解读this

    在<JavaScript深入之执行上下文栈>中讲到,当JavaScript代码执行一段可执行代码(executable code)时,会创建对应的执行上下文(execution conte ...

  8. The Secret Life of Types in Swift

    At first glance, Swift looked different as a language compared to Objective-C because of the modern ...

  9. c++、oc、swift初步评价

    c++是面向对象的多态语言: oc是面向对象的动态语言: swift是面向对象.面向协议.高阶类型.函数式编程语言:

  10. AlexNet梳理

    创新点 成功的使用relu函数替代了sigmoid函数,解决了使用sigmoid的梯度消散问题 成功的在全连接层使用dropout 成功的使用重叠最大池化 提出了LRN 利用GPU进行运算 数据增强2 ...