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. Java自动化环境搭建笔记(2)

    Java自动化环境搭建笔记(2) 自动化测试 在笔记一中已经完成了一键构建项目.xml指定规划测试集.数据解耦与allure报告生成的开发.接下来便是: 浏览器驱动通过配置启动 页面元素定位解耦,通过 ...

  2. python的模块放在哪里

    python程序中使用 import XXX 时,python解析器会在当前目录.已安装和第三方模块中搜索 xxx,如果都搜索不到就会报错. 使用sys.path.append()方法可以临时添加搜索 ...

  3. ANDROID - 打包和引用本地的AAR

        打包方法: 打开Gradle面板 Gradle Projects: 选择Library对应的Gradle Task,比如:":testsdk": 依次 Tasks > ...

  4. MySQL中的内连接、左连接、右连接、全连接、交叉连接

    创建两个表(a_table.b_table),两个表的关联字段分别为:a_table.a_id和b_table.b_id CREATE TABLE a_table ( a_id int NOT NUL ...

  5. excel双击下拉制作(以及双击下拉字符超限处理)

    最近,在项目的开发过程中,遇到了一个问题,自己要修改代码中的excel模板,有些列要处理成双击下拉的形式. excel制作双击下拉: 当然,我想,这对于大家来说是不难的,好实现,但是,我在制作的过程中 ...

  6. SpringBoot整合ActiveMQ发送邮件

    虽然ActiveMQ以被其他MQ所替代,但仍有学习的意义,本文采用邮件发送的例子展示ActiveMQ 1. 生产者1.1 引入maven依赖1.2 application.yml配置1.3 创建配置类 ...

  7. 如何在你的springboot(cloud)项目中引入我的github上的jar仓库呢?

    1. 将此标签内容放到pom.xml仅次于project标签下 <repositories> <repository> <id>github</id> ...

  8. 洛谷P4170 [CQOI2007]涂色题解

    废话: 这个题我第一眼看就是贪心呐, 可能是我之前那做过一道类似的题这俩题都是关于染色的 现在由于我帅气无比的学长的指导, 我已经豁然开朗, 这题贪心不对啊, 当时感觉自己好厉害贪心都能想出来 差点就 ...

  9. Macbook Pro升级10.15后百度网盘无法登陆,网络连接错误(-1001)

    兴冲冲升级到10.15,结果百度网盘挂了~QQ 由于长期在境外实验室做研究,百度又封锁了境外登陆,所以客户端是唯一跟家里联络的方式,现在它也挂了感觉整个天都塌下来了. 找了一圈,发现一个特别神奇的解锁 ...

  10. 【luoguP2989】[USACO10MAR]对速度的需要Need For Speed

    题目描述 最大化平均值 二分一个\(x\) \(check\): \(\frac{F+\sum_{i=1}^{n} X_{i} \times F_{i}}{M+\sum_{i=1}^{n} X_{i} ...