【Java】导出word文档之freemarker导出
Java导出word文档有很多种方式,本例介绍freemarker导出,根据现有的word模板进行导出
一、简单导出(不含循环导出)
1、新建一个word文件。如下图:

2、使用word将文件另存为xml的格式

3、编辑xml文件内容,将'用户名'替换成-> ${username}、'简介'替换成-> ${resume}、将图片内容用变量-> ${img}替换。

--》
4、修改xml文件后缀名,将xml修改为ftl格式。

5、使用java代码,完成word文件导出,需要使用到freemarker.jar包,maven依赖如下:
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
package com.test.word; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import sun.misc.BASE64Encoder; public class Test { public static void main(String[] args) throws IOException, TemplateException { // 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
Map<String, String> dataMap = new HashMap<String, String>();
dataMap.put("username", "张三");
dataMap.put("resume", "我是谁?");
dataMap.put("img", getImageStr()); // Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8"); /*
* 以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
// 指定路径的第一种方式(根据某个类的相对路径指定)
// configuration.setClassForTemplateLoading(this.getClass(),""); // 指定路径的第二种方式,我的路径是C:/a.ftl
configuration.setDirectoryForTemplateLoading(new File("C:/Users/H__D/Desktop/")); // 输出文档路径及名称
File outFile = new File("C:/Users/H__D/Desktop/test.doc"); // 以utf-8的编码读取ftl文件
Template t = configuration.getTemplate("简历.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close(); } /**
* 将图片转换成base64编码
* @return
*/
public static String getImageStr() {
String imgFile = "C:/Users/H__D/Desktop/IMG_0109.JPG";
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imgFile);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
BASE64Encoder encoder = new BASE64Encoder();
return encoder.encode(data);
} }
6、打开test.doc,如下:

二、带循环导出
1、新建一个带循环的word 文件,如下:

2、使用word将文件另存为xml的格式
3、编辑xml文件内容,用<#list userList as user> </#list>标签将循环标签包围起来(userList是集合的key, user是集合中的每个元素, 类似<c:forEach items='userList' var='user'>), 如图:

4、修改xml文件后缀名,将xml修改为ftl格式。
5、使用java代码,完成word文件导出,如下:
package com.test.word; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException; public class Test2 { public static void main(String[] args) throws IOException, TemplateException { // 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
Map<String, List> dataMap = new HashMap<String, List>(); List<User> list = new ArrayList<User>();
for(int i=0;i<5;i++){
User user = new User();
user.setName("hd"+(i+1));
list.add(user);
}
dataMap.put("userList", list); // Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8"); /*
* 以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
// 指定路径的第一种方式(根据某个类的相对路径指定)
// configuration.setClassForTemplateLoading(this.getClass(),""); // 指定路径的第二种方式,我的路径是C:/a.ftl
configuration.setDirectoryForTemplateLoading(new File("C:/Users/H__D/Desktop/")); // 输出文档路径及名称
File outFile = new File("C:/Users/H__D/Desktop/test2.doc"); // 以utf-8的编码读取ftl文件
Template t = configuration.getTemplate("循环.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close(); } }
package com.test.word;
public class User {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
6、打开test2.doc,如下:

【Java】导出word文档之freemarker导出的更多相关文章
- C# 导出word文档及批量导出word文档(1)
这里用到了两个dll,一个是aspose.word.dll,另外一个是ICSharpCode.SharpZipLib.dll,ICSharpCode.SharpZipLib.dll是用于批量 ...
- C# 导出word文档及批量导出word文档(4)
接下来是批量导出word文档和批量打印word文件,批量导出word文档和批量打印word文件的思路差不多,只是批量打印不用打包压缩文件,而是把所有文件合成一个word,然后通过js来调用 ...
- C# 导出word文档及批量导出word文档(3)
在初始化WordHelper时,要获取模板的相对路径.获取文档的相对路径多个地方要用到,比如批量导出时要先保存文件到指定路径下,再压缩打包下载,所以专门写了个关于获取文档的相对路径的类. #regio ...
- C# 导出word文档及批量导出word文档(2)
aspose.word主要是通过把读取出来的数据放到datatable里,在datable里做相应的格式的调整,再导出到word文档里.mvc和webform最后导出的语句略有不同,在mvc的cont ...
- 使用FreeMarker导出word文档(支持导出图片)
一.添加maven依赖,导入FreeMarker所需要的jar包 <dependency> <groupId>org.freemarker</groupId> &l ...
- Java 用Freemarker完美导出word文档(带图片)
Java 用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...
- 【Java】用Freemarker完美导出word文档(带图片)
Java 用Freemarker完美导出word文档(带图片) 前言 最近在项目中,因客户要求,将页面内容(如合同协议)导出成word,在网上翻了好多,感觉太乱了,不过最后还是较好解决了这个问题. ...
- Java使用freemarker导出word文档
通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...
- freemarker导出word文档——WordXML格式解析
前不久,公司一个项目需要实现导出文档的功能,之前是一个同事在做,做了3个星期,终于完成了,但是在项目上线之后却发现导出的文档有问题,此时,这个同事已经离职,我自然成为接班者,要把导出功能实现,但是我看 ...
随机推荐
- CentOS ln 链接
ln -s /u01/app/oracle/product/11.2.0/dbhome_1/bin/sqlplus/ /usr/bin (解决bash: sqlplus: command not fo ...
- 22. Generate Parentheses (backTracking)
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- vue 添加子路由,并对路由重定向
// 用户信息首页 { path: '/user/index', name: 'userIndex', component: userIndex, redirect: '/user/index/sho ...
- 【Android内存泄漏检测】LeakCanary使用总结
一.什么是LeakCanary? LeakCanary就是用来检测Android端内存泄漏的一个工具.能够检测Activity的泄漏 什么是内存泄漏? Java 对象有时也会”长死不死“,GC 拿它没 ...
- 记录下ABAP开发的一些东西(T-code居多)Updated to markdown
几个TCODE se38 开发program,report: sa38 只运行program se37 开发function: se11/se16 管理数据字典/数据表: ko03 Internal ...
- [leetcode]678. Valid Parenthesis String验证有效括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- static 构造函数的认识
最近,看到一道面试题,如下 class Class1 { ; static Class1() { count++; } public Class1() { count++; } } Class1 on ...
- iOS 网络操作与AFNetworking
一.早前的几个网络框架 1.ASI框架: HTTP终结者.很牛, 但是有BUG, 已经停止更新. 2.MKNetworkKit (印度人写的). 3.AFN一直还在更新. AFNetworking的出 ...
- 基于vue的悬浮碰撞窗口(用于打广告的)组件
由于项目需要改写了一个悬浮碰撞弹窗组件 <template> <div class="floatLayer"> <a class="clos ...
- ES6 Symbol的应用场景
一.简介 具体使用请参考:API ES6 引入了一种新的原始数据类型Symbol,表示独一无二的值.它是 JavaScript 语言的第七种数据类型,前六种是:undefined.null.布尔值(B ...