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,如下:
    

原文地址:https://www.cnblogs.com/wangjintao-0623/p/10496443.html