一、简单导出(不含循环导出)

  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的更多相关文章

  1. java用freemarker导出数据到word(含多图片)

    一.制作word模版 新建word文档,按照需要设置好字体等各种格式:这里为了显得整齐使用了无边框的表格. 将word文档另存为xml文件(注意不是word xml文档,我吃了这家伙的大亏了) 然后用 ...

  2. 利用freemarker导出页面格式复杂的excel

    刚开始大家可能会利用poi生成简单的excel,但是遇到需要生成复杂的excel,poi导出excel就比较困难,这时候可以利用freemarker来渲染实现实现生成复杂的excel, 首先,将exc ...

  3. JAVA利用JXL导出/生成 EXCEL

    /** * 导出导出采暖市场部收入.成本.利润明细表 * @author JIA-G-Y */ public String exporExcel(String str) { String str=Se ...

  4. java通过freemarker导出包含富文本图片的word文档

    废话不多说,进入正题! 本文重点在于:对富文本图片的导出(基础的freemarker+word模板导出这里不做详细解说哈) 参考文章:http://www.cnblogs.com/liaofeifig ...

  5. Java用freemarker导出word

    概述 最近一个项目要导出word文档,折腾老半天,发现还是用freemarker的模板来搞比较方便省事,现总结一下关键步骤,供大家参考,这里是一个简单的试卷生成例子. 详细 代码下载:http://w ...

  6. Java用freemarker导出Word 文档

    1.用Microsoft Office Word打开word原件: 2.把需要动态修改的内容替换成***,如果有图片,尽量选择较小的图片几十K左右,并调整好位置: 3.另存为,选择保存类型Word 2 ...

  7. Java使用freemarker导出word文档

    通过freemarker,以及JAVA,导出word文档. 共分为三步: 第一步:创建模板文件 第二步:通过JAVA创建返回值. 第三步:执行 分别介绍如下: 第一步: 首先创建word文档,按照想要 ...

  8. JAVA利用JXL导出/生成 EXCEL1

    /** * 导出导出采暖市场部收入.成本.利润明细表 * @author JIA-G-Y */ public String exporExcel(String str) { String str=Se ...

  9. java利用poi导出数据到excel

    背景: 上一篇写到利用jtds连接数据库获取对应的数据,本篇写怎样用poi将数据到处到excel中,此程序为Application 正文: 第三方poi jar包:poi驱动包下载 代码片段: /** ...

随机推荐

  1. mac常用操作:

    Mac常用软件需要熟悉 常用操作: command + w 关闭窗口  + n 最小化当前窗口  + m 关闭所有窗口  +  + w command + c 复制 command + v 粘贴 co ...

  2. mutex锁住共用线程函数 造成了死锁 ,为什么?

    锁住共用的线程函数,为什么出现了死锁的现象,是真的死锁了吗?为什么勒[清晰早点] [逍遥游]# 一般都是用 EnterCriticalSection 和 LeaveCriticalSection 锁住 ...

  3. 120. Triangle(动态规划 三角形最小路径 难 想)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. c++第二十四天

    p126~p128: 1.关系运算符作用于算数类型和指针类型. 2.逻辑运算符作用于任意能转换成布尔类型值的类型. 3.以上两种运算的运算对象和运算结果都是右值. 4.逻辑与和逻辑或的运算策略:短路求 ...

  5. MySQL事务概述-1

    事务是数据库区别于文件系统最重要的特性之一.事务可由一条非常简单的SQL语句组成,也可以由一组复杂的SQL语句组成.事务是访问并更新数据库中各种数据项的一个程序执行单元.在事务操作中,要么都做修改,要 ...

  6. mailx配置安装

    mailxwget http://ftp.debian.org/debian/pool/main/h/heirloom-mailx/heirloom-mailx_12.5.orig.tar.gztar ...

  7. 使用NodeJS将文件或图像上传到服务器

    原文链接:http://www.codeceo.com/article/nodejs-upload-file-to-server.html

  8. 20145201李子璇《网络对抗》PC平台逆向破解

    20145201<网络对抗>PC平台逆向破解 准备阶段 下载安装execstack. 获取shellcode的c语言代码 设置堆栈可执行 将环境设置为:堆栈可执行.地址随机化关闭(2开启, ...

  9. 【转】为什么我的DIV块前总有空隙?

    在做网站项目时,博主爱吾所爱(爱生活=爱技术)很偶然地碰到一个奇怪的事情.我的DIV嵌套在另一个DIV里,总是出现这样一行空隙: Firebug查看内外两层DIV的margin, padding, l ...

  10. Flask 4 拓展

    NOTE 1.Flask被设计为可拓展模式,所以没有提供如数据库和用户认证等重要的功能,允许开发者按需开发. 2.使用Flask-Script支持命令行选项: 安装flask-script: pip ...