本文記錄使用freemarker技術(shù)在web后臺導(dǎo)出word文檔的過程柑营。
整個過程分為以下幾步。后面會用一個例子加以說明。
- 生成word模版
- 修改ftl模版
- 填充數(shù)值繁成,導(dǎo)出word模板
-
生成Word模板
生產(chǎn)word模版主要分為兩步,一個是把word文檔另存為xml文檔己儒,注意是另存為崎岂。然后把xml文檔后綴改為ftl文檔。在編輯word文檔的時候闪湾,最好把需要填充或替換的位置冲甘,以一種特殊的標(biāo)識符替代。不然后面編輯ftl模版就很痛苦途样。下面舉個例江醇。
原始模版.png
里面的Tb1開頭就是我們需要替換的占位字段,這里的占位字段不要太復(fù)雜何暇,比如${Tb123}陶夜,因為后面另存為xml后,多種類型的字符會發(fā)生拆分裆站。
-
編輯ftl模版
在文本編輯器里面打開ftl模版条辟。然后搜索在word里填充的占位符。然后替換成后面能被替換的格式宏胯。比如羽嫡,把Tb111,替換成${tb111}肩袍。這里可能原始的占位符發(fā)生了截斷杭棵,比如本來是Tb111,但是在ftl模版里面成了Tb1~~<dafda<11氛赐。這時就要把中間的那些刪掉颜屠。最后替換后的格式就是標(biāo)準(zhǔn)的可替換參數(shù)。
ftl模版.png - 修改代碼
這塊基本邏輯就是生產(chǎn)參數(shù)鹰祸,填充到模板甫窟。
<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 設(shè)置路徑加載方式。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("導(dǎo)出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");
}
}
最后導(dǎo)出的文檔樣式如下粗井。
導(dǎo)出文檔.png