日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

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

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

使用FreeMarker導(dǎo)出固定格式word文檔

一.下載FreeMarker的jar包

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

二.修改word文檔

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

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

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

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

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

五.對于集合元素

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

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

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

    七.創(chuàng)建導(dǎo)出工具類

    將上一步生產(chǎn)的resume.ftl文件,復(fù)制到工具包下,創(chuàng)建工具類用于導(dǎo)出word,直接復(fù)制如下代碼:

    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; } }

    八.控制層寫法

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

    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);}//投票產(chǎn)生的人數(shù)String expertNumber= ?;map.put("expertNumber", expertNumber);//總?cè)藬?shù)String number = ?;map.put("number", number);String filename = ?;//可以是中文// 提示:在調(diào)用工具類生成Word文檔之前應(yīng)當(dāng)檢查所有字段是否完整 // 否則Freemarker的模板殷勤在處理時可能會因為找不到值而報錯 這里暫時忽略這個步驟了 File file = null; InputStream fin = null; ServletOutputStream out = null; try { // 調(diào)用工具類WordGenerator的createDoc方法生成Word文檔 file = WordGenerator.createDoc(map, "resume"); fin = new FileInputStream(file); resp.setCharacterEncoding("utf-8"); resp.setContentType("application/msword"); // 設(shè)置瀏覽器以下載的方式處理該文件默認(rèn)名為filename.doc String fileName = "attachment; filename= "+new String(filename.getBytes(), "ISO-8859-1")+".doc";//轉(zhuǎn)化中文,防止中文文件名導(dǎo)致下載文件文件名異常或者沒有后綴名resp.addHeader("Content-Disposition", fileName); out = resp.getOutputStream(); byte[] buffer = new byte[512]; // 緩沖區(qū) int bytesToRead = -1; // 通過循環(huán)將讀入的Word文件的內(nèi)容輸出到瀏覽器中 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寫法

    頁面調(diào)用一個同步方法,到控制層即可導(dǎo)出文件。

    10.生成效果

    總結(jié)

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

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。