本文记录使用freemarker技术在web后台导出word文档的过程。
整个过程分为以下几步。后面会用一个例子加以说明。
- 生成word模版
- 修改ftl模版
- 填充数值,导出word模板
-
生成Word模板
生产word模版主要分为两步,一个是把word文档另存为xml文档,注意是另存为。然后把xml文档后缀改为ftl文档。在编辑word文档的时候,最好把需要填充或替换的位置,以一种特殊的标识符替代。不然后面编辑ftl模版就很痛苦。下面举个例。
原始模版.png
里面的Tb1开头就是我们需要替换的占位字段,这里的占位字段不要太复杂,比如${Tb123},因为后面另存为xml后,多种类型的字符会发生拆分。
-
编辑ftl模版
在文本编辑器里面打开ftl模版。然后搜索在word里填充的占位符。然后替换成后面能被替换的格式。比如,把Tb111,替换成${tb111}。这里可能原始的占位符发生了截断,比如本来是Tb111,但是在ftl模版里面成了Tb1~~<dafda<11。这时就要把中间的那些删掉。最后替换后的格式就是标准的可替换参数。
ftl模版.png
- 修改代码
这块基本逻辑就是生产参数,填充到模板。
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.28</version>
</dependency>
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import freemarker.template.Configuration;
import freemarker.template.Template;
public class WordExportUtil {
private static Logger LOGGER = LoggerFactory.getLogger(WordExportUtil.class);
private static WordExportUtil service = null;
private WordExportUtil() {
super();
}
public static WordExportUtil getInstance() {
if(service == null) {
synchronized(WordExportUtil.class){
if(service == null) {
service = new WordExportUtil();
}
}
}
return service;
}
/**
*
* @param templateFilePath eg: /template/test/test.ftl
* @param dataMap
* @param exportFilePath eg: /tmp/test/test123.doc
* @param loadType 设置路径加载方式。1-绝对路径,2-项目相对路径
* @return
* @throws Exception
*/
public File createDocFile(String templateFilePath,Map<String, Object> dataMap, String exportFilePath, int loadType) throws Exception {
Template t = null;
Configuration configuration = new Configuration(Configuration.VERSION_2_3_28);
configuration.setDefaultEncoding("UTF-8");
try {
templateFilePath = pathReplace(templateFilePath);
String ftlPath = templateFilePath.substring(0, templateFilePath.lastIndexOf("/"));
if(loadType == 1) {
configuration.setDirectoryForTemplateLoading(new File(ftlPath)); // FTL文件所存在的位置
}else {
configuration.setClassForTemplateLoading(this.getClass(), ftlPath);//以类加载的方式查找模版文件路径
}
String ftlFile = templateFilePath.substring(templateFilePath.lastIndexOf("/")+1);
t = configuration.getTemplate(ftlFile); // 模板文件名
File outFile = new File(exportFilePath);
Writer out = null;
out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));
t.process(dataMap, out);
} catch (Exception e) {
LOGGER.error("导出word文档出错", e);
throw e;
}
return null;
}
/**
* 把路径的\替换成/
* @param path
* @return
*/
private String pathReplace(String path) {
while(path != null && path.contains("\\")) {
path = path.replace("\\", "/");
}
return path;
}
public static void main(String[] args) {
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
String templateFile = "C:\\Users\\luke\\Desktop\\tmpStore\\exportTemplate.ftl";
String exportFile = "C:\\Users\\luke\\Desktop\\tmpStore\\luedf.doc";
try {
WordExportUtil.getInstance().createDocFile(templateFile, dataMap, exportFile, 1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 测试用的
* @param dataMap
*/
public static void getData(Map<String, Object> dataMap) {
dataMap.put("tb11", "10");
dataMap.put("tb12", "2012");
dataMap.put("tb13", "2");
dataMap.put("tb111", "13");
dataMap.put("tb112", "13");
dataMap.put("tb113", "13");
dataMap.put("tb114", "13");
dataMap.put("tb115", "13");
dataMap.put("tb116", "13");
dataMap.put("tb117", "13");
dataMap.put("tb118", "13");
dataMap.put("tb119", "13");
dataMap.put("tb1110", "13");
dataMap.put("tb1111", "13");
}
}
最后导出的文档样式如下。

网友评论