因為業務需求,需要導出word 文檔,效果如下:

上述字段 每一行為list 遍歷得到
技術: freemarker 技術---我們word 高級版本(ftl 的制作)
1 首先要准備ftl 文檔
打開word 編輯,變為下面形式(注意,下面為表格插入,一行一列)

然后將word 保存為xml 文檔
![]()
打開xml 文檔,下面我們就更改四處,
一加入 <w:tr>前<#list list as list> 第一個list 不能變 第二個list 為變量名 第三個list 為別名
二將${fieldname} 改為${list.fieldname}
三將${fieldname} 改為${list.fieldname}
四</w:tr>后加入</#list>
注意:下面為省略的代碼 代碼格式化 --- notepad++ 打開-插件-XMLTools-pretty print開頭的全點擊
<#list list as list> <w:tr> <w:tblPrEx> <w:tblBorders> ....... <w:rPr> <w:rFonts w:hint="eastAsia"/> <w:lang w:val="en-US" w:eastAsia="zh-CN"/> </w:rPr> <w:t>${list.fieldname}</w:t> </w:r> </w:p> ....... <w:rPr> <w:rFonts w:hint="eastAsia"/> <w:lang w:val="en-US" w:eastAsia="zh-CN"/> </w:rPr> <w:t>${list.field}</w:t> </w:r> </w:p> <w:p> </w:p> </w:tc> </w:tr> </#list>
將改好的xml 保存,如果notepad++報錯不用理會,將xml 改為ftl 文件,就得到我們需要的ftl了,編譯后的不要用word打開
2 java 代碼的實現
引入jar 包
<properties> <!--freemarker--> <freemarker.version>2.3.23</freemarker.version> </properties> <dependencies> <!--freemarker--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>${freemarker.version}</version> </dependency>
ftl 文檔存放

代碼書寫
package export;
import java.io.*;
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;
import sun.misc.BASE64Encoder;
public class DocumentHandler {
private Configuration configuration = null;
public DocumentHandler() {
configuration = new Configuration();
configuration.setDefaultEncoding("utf-8");
}
public void createDoc() {
// 要填入模本的數據文件
Map dataMap = new HashMap();
getData(dataMap);
// getTest(dataMap);
// 設置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法。可以重servlet,classpath,數據庫裝載,
// 這里我們的模板是放在com.template包下面
configuration.setClassForTemplateLoading(this.getClass(),
"/com/template");
Template t = null;
try {
// test.ftl為要裝載的模板
t = configuration.getTemplate("3.ftl");
t.setEncoding("utf-8");
} catch (IOException e) {
e.printStackTrace();
}
// 輸出文檔路徑及名稱
File outFile = new File("D:/test3.doc","utf-8"); //要與上下文編碼一致
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile), "utf-8"));
} catch (Exception e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
out.close();
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 注意dataMap里存放的數據Key值要與模板中的參數相對應
* @param dataMap
*
*/
@SuppressWarnings("unchecked")
private void getData(Map dataMap) {
dataMap.put("name", "表格設計的合理性美觀性要考慮");
List<Map<String, Object>> newsList=new ArrayList<Map<String,Object>>();
for(int i=1;i<=5;i++){
Map<String, Object> map=new HashMap<String, Object>();
map.put("fieldname", "字段姓名"+i);
map.put("field", "字段內容"+i);
newsList.add(map);
}
dataMap.put("list",newsList); //注意list 的名字
}
生成文檔
package export; public class Export { public static void main(String[] args) { DocumentHandler dh=new DocumentHandler(); dh.createDoc(); System.out.println("end"); } }
結束
后記,導出圖片
dataMap.put("image", getImageStr());
1 private String getImageStr() { 2 String imgFile = "d:/1.png"; 3 InputStream in = null; 4 byte[] data = null; 5 try { 6 in = new FileInputStream(imgFile); 7 data = new byte[in.available()]; 8 in.read(data); 9 in.close(); 10 } catch (IOException e) { 11 e.printStackTrace(); 12 } 13 BASE64Encoder encoder = new BASE64Encoder(); 14 return encoder.encode(data); 15 } 16
