日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用FreeMarker导出固定格式word文档

發布時間:2024/3/13 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用FreeMarker导出固定格式word文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用FreeMarker導出固定格式word文檔

一.下載FreeMarker的jar包

下載地址:http://freemarker.org/freemarkerdownload.html,導入項目,報錯的自行搜索缺少的jar包。

二.修改word文檔

將需要替換的使用freemarker的占位符替換,類似EL表達式,不同是的對于循環遍歷,if判斷,freemarker有自己的格式。對于需要對list遍歷生成的,word文檔里使用參變量,后面在xml文件里額外定義參變量。修改后的具體格式如下:

三.將修改后的word文檔保存成xml格式

使用word編輯好后,保存文檔,將文檔另存為xml格式,具體如下圖:

四.使用編輯工具打開xml,修改被分割的占位符

使用notepad或者visulcode打開xml文檔,將占位符被分割的,刪除中間的word標簽,只有完整占位符才會被freemarker解析。

五.對于集合元素

首先在xml中,需要確定循環的具體循環體,防止循環結果錯誤,找到正確循環體后,在頭和尾部額外插入標簽。<#list 集合 as 參數名> </#list >,具體效果如下:

  • 需要循環生成選出的人員名+空格,而不是一整行。<w:p>代表一行
  • 需要循環一行生成人員名。<w:tr>代表一個表格tr
  • 六.將修改好的xml文檔,保存后,直接修改文件名,保存成resume.ftl

    需要修改后綴名,window詢問是否保存,點擊是,即可。

    七.創建導出工具類

    將上一步生產的resume.ftl文件,復制到工具包下,創建工具類用于導出word,直接復制如下代碼:

    package com.yh.common.util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; public class WordGenerator {private static Configuration configuration = null; private static Map<String, Template> allTemplates = null; static { configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); configuration.setClassForTemplateLoading(WordGenerator.class, "/com/yh/common/util"); allTemplates = new HashMap<>(); // Java 7 鉆石語法 try { allTemplates.put("resume", configuration.getTemplate("resume.ftl")); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } private WordGenerator() { throw new AssertionError(); } public static File createDoc(Map<?, ?> dataMap, String type) { String name = "temp" + (int) (Math.random() * 100000) + ".doc"; File f = new File(name); Template t = allTemplates.get(type); try { // 這個地方不能使用FileWriter因為需要指定編碼類型否則生成的Word文檔會因為有無法識別的編碼而無法打開 Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8"); t.process(dataMap, w); w.close(); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex); } return f; } }

    八.控制層寫法

    開發時,使用的struct,提供struct的寫法。其中父類中包括了獲取response和request的方法,這邊可以在方法參數里面添加,讀者根據具體情況自行修改。

    public String exportWord() throws ServletException, IOException { HttpServletResponse resp = ServletActionContext.getResponse();request.setCharacterEncoding("utf-8"); Map<String, Object> map = new HashMap<String, Object>(); //查詢評審場次信息List list1 = ?;if(!ValidateUtil.isEmpty(list1)){map.put("list1", list1);}//查詢輪次信息map.put("round", ?);List list2 = ?;if(!ValidateUtil.isEmpty(list2)){map.put("list2", list2);}List list3 = ?;if(!ValidateUtil.isEmpty(list3)){map.put("list3", list3);}//投票產生的人數String expertNumber= ?;map.put("expertNumber", expertNumber);//總人數String number = ?;map.put("number", number);String filename = ?;//可以是中文// 提示:在調用工具類生成Word文檔之前應當檢查所有字段是否完整 // 否則Freemarker的模板殷勤在處理時可能會因為找不到值而報錯 這里暫時忽略這個步驟了 File file = null; InputStream fin = null; ServletOutputStream out = null; try { // 調用工具類WordGenerator的createDoc方法生成Word文檔 file = WordGenerator.createDoc(map, "resume"); fin = new FileInputStream(file); resp.setCharacterEncoding("utf-8"); resp.setContentType("application/msword"); // 設置瀏覽器以下載的方式處理該文件默認名為filename.doc String fileName = "attachment; filename= "+new String(filename.getBytes(), "ISO-8859-1")+".doc";//轉化中文,防止中文文件名導致下載文件文件名異常或者沒有后綴名resp.addHeader("Content-Disposition", fileName); out = resp.getOutputStream(); byte[] buffer = new byte[512]; // 緩沖區 int bytesToRead = -1; // 通過循環將讀入的Word文件的內容輸出到瀏覽器中 while((bytesToRead = fin.read(buffer)) != -1) { out.write(buffer, 0, bytesToRead); } } finally { if(fin != null) fin.close(); if(out != null) out.close(); if(file != null) file.delete(); // 刪除臨時文件 } return SUCCESS;}

    九.jsp寫法

    頁面調用一個同步方法,到控制層即可導出文件。

    10.生成效果

    總結

    以上是生活随笔為你收集整理的使用FreeMarker导出固定格式word文档的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。