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

  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. ubuntu16.04(liunx) 离线安装 xgboost (anaconda3,anaconda2共存)

    服务器ubuntu 系统同时安装了 anaconda3,anaconda2 ,但服务器没有连接外网,所以 想在python3 环境下安装离线安装xgboost. 主要分2步: 0:进入py3环境  ( ...

  2. FTP服务器配置实践

    1.为linux系统分配IP地址:192.168.X.1/24,并重启网络服务,客户端XP系统IP地址为:192.168.X.2/24, 2.查询本机是否安装了vsftpd服务,结果显示未安装,挂载光 ...

  3. UnicodeDecodeError: 'ascii' codec can't decode byte 0xbb in position 51: ord

    1.问题描述:一个在Django框架下使用Python编写的定时更新项目,在Windows系统下测试无误,在Linux系统下测试,报如下错误: ascii codec can't decode byt ...

  4. 从官网学习Node.js FS模块方法速查

    最新文档请查看仓库 https://github.com/wangduandu... 1. File System 所有文件操作提供同步和异步的两种方式,本笔记只记录异步的API 异步方式其最后一个参 ...

  5. 20145312《网络对抗》Exp4 恶意代码分析

    20145312<网络对抗>Exp4 恶意代码分析 问题回答 1.总结一下监控一个系统通常需要监控什么.用什么来监控. 监控一个系统通常需要监控这个系统的注册表,进程,开放端口,程序服务还 ...

  6. InstallShield 2015 Premier的Basic MSI Project如何在卸载时删除残留的文件 (转)

    转载:http://blog.csdn.net/zztoll/article/details/54018615#comments 先说下缘由,我在用InstallShield 2015 Premier ...

  7. CF #505 B Weakened Common Divisor(数论)题解

    题意:给你n组,每组两个数字,要你给出一个数,要求这个是每一组其中一个数的因数(非1),给出任意满足的一个数,不存在则输出-1. 思路1:刚开始乱七八糟暴力了一下果断超时,然后想到了把每组两个数相乘, ...

  8. luogu P1025 数的划分

    https://www.luogu.org/problem/show?pid=1025 n的k划分 且不出现划分成0的情况  可以 分为两种情况 所有划分的数 都大于1的情况 至少划分的数里面有1的情 ...

  9. [微信开发] - UnionID以及微信开放平台

  10. install ros-indigo-laser-geometry

    -- Using these message generators: gencpp;genlisp;genpy CMake Warning at /opt/ros/indigo/share/catki ...