java利用freemarker导出world
一、简单导出(不含循环导出)
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利用freemarker导出world的更多相关文章
- java用freemarker导出数据到word(含多图片)
一.制作word模版 新建word文档,按照需要设置好字体等各种格式:这里为了显得整齐使用了无边框的表格. 将word文档另存为xml文件(注意不是word xml文档,我吃了这家伙的大亏了) 然后用 ...
- 利用freemarker导出页面格式复杂的excel
刚开始大家可能会利用poi生成简单的excel,但是遇到需要生成复杂的excel,poi导出excel就比较困难,这时候可以利用freemarker来渲染实现实现生成复杂的excel, 首先,将exc ...
- JAVA利用JXL导出/生成 EXCEL
/** * 导出导出采暖市场部收入.成本.利润明细表 * @author JIA-G-Y */ public String exporExcel(String str) { String str=Se ...
- java通过freemarker导出包含富文本图片的word文档
废话不多说,进入正题! 本文重点在于:对富文本图片的导出(基础的freemarker+word模板导出这里不做详细解说哈) 参考文章:http://www.cnblogs.com/liaofeifig ...
- Java用freemarker导出word
概述 最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子. 详细 代码下载:http://w ...
- Java用freemarker导出Word 文档
1.用Microsoft Office Word打开word原件: 2.把需要动态修改的内容替换成***,如果有图片,尽量选择较小的图片几十K左右,并调整好位置: 3.另存为,选择保存类型Word 2 ...
- Java使用freemarker导出word文档
通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...
- JAVA利用JXL导出/生成 EXCEL1
/** * 导出导出采暖市场部收入.成本.利润明细表 * @author JIA-G-Y */ public String exporExcel(String str) { String str=Se ...
- java利用poi导出数据到excel
背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...
随机推荐
- java基础类型中的char和byte的辨析及Unicode编码和UTF-8的区别
在平常工作中使用到char和byte的场景不多,但是如果项目中使用到IO流操作时,则必定会涉及到这两个类型,下面让我们一起来回顾一下这两个类型吧. char和byte的对比 byte byte 字节, ...
- Action<>和Func<> 区别
其实他们两个都是委托[代理]的简写形式. 一.[action<>]指定那些只有输入参数,没有返回值的委托 Delegate的代码: public delegate void myDeleg ...
- 2018-2019 ACM-ICPC Nordic Collegiate Programming Contest (NCPC 2018) Solution
A. Altruistic Amphibians Upsolved. 题意: $有n只青蛙,其属性用三元组表示 <l_i, w_i, h_i> l_i是它能跳的高度,w_i是它的体重,h_ ...
- Learning to Rank之Ranking SVM 简介
排序一直是信息检索的核心问题之一,Learning to Rank(简称LTR)用机器学习的思想来解决排序问题(关于Learning to Rank的简介请见我的博文Learning to Rank简 ...
- 【android】开发中遇到的一些问题
1:华为输入法,输入框为ACTION_DONE模式,ActionId是 UNSPECIFIED EditText对象.setImeOptions(EditorInfo.IME_ACTION_DONE) ...
- Flask 使用富文本输入框
模板 <script src="{{ url_for('static', filename='ckeditor/ckeditor.js') }}"></scrip ...
- lucas 快速求大数组合数
根据公式就是 对每次C(n,m) = C(n%p,m%p) * C(n/p,m/p); ll pow(ll x,ll n) { ll res = ; x%=mod; while (n) { ) re ...
- 树上启发式合并(dsu on tree)学习笔记
有丶难,学到自闭 参考的文章: zcysky:[学习笔记]dsu on tree Arpa:[Tutorial] Sack (dsu on tree) 先康一康模板题吧:CF 600E($Lomsat ...
- python 字典元素值的乘积
my_dict = {,,} result= for key in my_dict: result=result * my_dict[key] print(result)
- mysql获取随机数据的方法
order by rand() 数据多了极慢,随机性非常好,适合非常小数据量的情况. 复制代码 代码如下: SELECT * FROM table_name AS r1 JOIN (SELECT (R ...