由於業務需求,須要導出word 文檔,效果以下:java
上述字段 每一行爲list 遍歷獲得數據庫
技術: freemarker 技術---咱們word 高級版本(ftl 的製做)this
1 首先要準備ftl 文檔編碼
打開word 編輯,變爲下面形式(注意,下面爲表格插入,一行一列)spa
而後將word 保存爲xml 文檔插件
打開xml 文檔,下面咱們就更改四處,設計
一加入 <w:tr>前<#list list as list> 第一個list 不能變 第二個list 爲變量名 第三個list 爲別名code
二將${fieldname} 改成${list.fieldname}xml
三將${fieldname} 改成${list.fieldname}blog
四</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