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

歡迎訪問 生活随笔!

生活随笔

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

Java用freemarker导出word

發(fā)布時間:2025/3/15 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java用freemarker导出word 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

概述

最近一個項目要導(dǎo)出word文檔,折騰老半天,發(fā)現(xiàn)還是用freemarker的模板來搞比較方便省事,現(xiàn)總結(jié)一下關(guān)鍵步驟,供大家參考,這里是一個簡單的試卷生成例子。

詳細(xì)

代碼下載:http://www.demodashi.com/demo/10264.html

最近一個項目要導(dǎo)出word文檔,折騰老半天,發(fā)現(xiàn)還是用freemarker的模板來搞比較方便省事,現(xiàn)總結(jié)一下關(guān)鍵步驟,供大家參考,這里是一個簡單的試卷生成例子。

一、模板的制作

先用Word做一個模板,如下圖:

(注意,上面是有表格的,我設(shè)置了邊框不可見)然后另存為XML文件,之后用工具打開這個xml文件,有人用firstobject XML Editor感覺還不如notepad++,我這里用notepad++,主要是有高亮顯示,和元素自動配對,效果如下:

上面黑色的地方基本是我們之后要替換的地方,比如xytitle替換為${xytitle},對已表格要十分注意,比如選擇題下面的表格,我們可以通過<w:tr>查找來定位,一對<w:tr></w:tr>代表一行,也就是一條記錄(一道題),我們這里要用一對<#list></#list>來將其包括,以便后續(xù)填充數(shù)據(jù),具體可參照Freemarker頁面語法,例如這里選擇題,我們是兩行為一條記錄,所以要<#list></#list>要包括兩行,形如:<#list table1 as plan1><w:tr>題號 題目</w:tr><w:tr>選項</w:tr></#list>,然后在這其中找著對應(yīng)的xzn,xztest,ans1,ans2,ans3,ans4替換為${plan1.xzn},${plan1.xztest},${plan1.ans1},${plan1.ans2},${plan1.ans3},${plan1.ans4},注意這里的table1及plan1命名,table1后續(xù)填充數(shù)據(jù)要用到,其他的替換同理操作,得到效果如下:

保存后,修改后綴名為ftl,至此模板制作完畢。

二、編程實(shí)現(xiàn)

這里用到了freemarker-2.3.13.jar包,代碼如下:

package common; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Map; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; public class DocumentHandler { private Configuration configuration = null; public DocumentHandler() { configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); } public void createDoc(Map<String,Object> dataMap,String fileName) throws UnsupportedEncodingException { //dataMap 要填入模本的數(shù)據(jù)文件 //設(shè)置模本裝置方法和路徑,FreeMarker支持多種模板裝載方法??梢灾豷ervlet,classpath,數(shù)據(jù)庫裝載, //這里我們的模板是放在template包下面 configuration.setClassForTemplateLoading(this.getClass(), "/template"); Template t=null; try { //test.ftl為要裝載的模板 t = configuration.getTemplate("fctestpaper.ftl"); } catch (IOException e) { e.printStackTrace(); } //輸出文檔路徑及名稱 File outFile = new File(fileName); Writer out = null; FileOutputStream fos=null; try { fos = new FileOutputStream(outFile); OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8"); //這個地方對流的編碼不可或缺,使用main()單獨(dú)調(diào)用時,應(yīng)該可以,但是如果是web請求導(dǎo)出時導(dǎo)出后word文檔就會打不開,并且包XML文件錯誤。主要是編碼格式不正確,無法解析。 //out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); out = new BufferedWriter(oWriter); } catch (FileNotFoundException e1) { e1.printStackTrace(); } try { t.process(dataMap, out); out.close(); fos.close(); } catch (TemplateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //System.out.println("---------------------------"); } }

然后是準(zhǔn)備數(shù)據(jù)調(diào)用就行,代碼如下:

package com.havenliu.document; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; public class Main { /** * @param args * @throws UnsupportedEncodingException */ public static void main(String[] args) throws UnsupportedEncodingException {; Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("xytitle", "試卷"); int index = 1; // 選擇題 List<Map<String, Object>> list1 = new ArrayList<Map<String, Object>>();//題目 List<Map<String, Object>> list11 = new ArrayList<Map<String, Object>>();//答案 index = 1; for (int i = 0; i < 5; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("xzn", index + "."); map.put("xztest", "( )操作系統(tǒng)允許在一臺主機(jī)上同時連接多臺終端,多個用戶可以通過各自的終端同時交互地使用計算機(jī)。"); map.put("ans1", "A" + index); map.put("ans2", "B" + index); map.put("ans3", "C" + index); map.put("ans4", "D" + index); list1.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("fuck", index + "."); map1.put("abc", "A" + index); list11.add(map1); index++; } dataMap.put("table1", list1); dataMap.put("table11", list11); // 填空題 List<Map<String, Object>> list2 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list12 = new ArrayList<Map<String, Object>>(); index = 1; for (int i = 0; i < 5; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("tkn", index + "."); map.put("tktest", "操作系統(tǒng)是計算機(jī)系統(tǒng)中的一個___系統(tǒng)軟件_______,它管理和控制計算機(jī)系統(tǒng)中的___資源_________."); list2.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("fill", index + "."); map1.put("def", "中級調(diào)度" + index); list12.add(map1); index++; } dataMap.put("table2", list2); dataMap.put("table12", list12); // 判斷題 List<Map<String, Object>> list3 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list13 = new ArrayList<Map<String, Object>>(); index = 1; for (int i = 0; i < 5; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("pdn", index + "."); map.put("pdtest", "復(fù)合型防火墻防火墻是內(nèi)部網(wǎng)與外部網(wǎng)的隔離點(diǎn),起著監(jiān)視和隔絕應(yīng)用層通信流的作用,同時也常結(jié)合過濾器的功能。"); list3.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("judge", index + "."); map1.put("hij", "對" + index); list13.add(map1); index++; } dataMap.put("table3", list3); dataMap.put("table13", list13); // 簡答題 List<Map<String, Object>> list4 = new ArrayList<Map<String, Object>>(); List<Map<String, Object>> list14 = new ArrayList<Map<String, Object>>(); index = 1; for (int i = 0; i < 5; i++) { Map<String, Object> map = new HashMap<String, Object>(); map.put("jdn", index + "."); map.put("jdtest", "說明作業(yè)調(diào)度,中級調(diào)度和進(jìn)程調(diào)度的區(qū)別,并分析下述問題應(yīng)由哪一級調(diào)度程序負(fù)責(zé)。"); list4.add(map); Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("answer", index + "."); map1.put("xyz", "說明作業(yè)調(diào)度,中級調(diào)度和進(jìn)程調(diào)度的區(qū)別,并分析下述問題應(yīng)由哪一級調(diào)度程序負(fù)責(zé)。"); list14.add(map1); index++; } dataMap.put("table4", list4); dataMap.put("table14", list14); MDoc mdoc = new MDoc(); mdoc.createDoc(dataMap, "E:/outFile.doc"); } }

注意上面map中的key必須和模板中的對應(yīng),否則會報錯。

三、項目導(dǎo)入

下載附件后,解壓后,看到如下目錄

將Document項目導(dǎo)入eclipse即可。導(dǎo)入后的效果如下:

會在e盤生成文件

四、運(yùn)行效果

打開生成的outFile.doc則看到如下的樣子:

五、freemarker語法

Freemarker頁面語法:http://blog.csdn.net/thismonth/article/details/5194982

?

代碼下載:http://www.demodashi.com/demo/10264.html

注:本文著作權(quán)歸作者,由demo大師發(fā)表,拒絕轉(zhuǎn)載,轉(zhuǎn)載需要作者授權(quán)

?

總結(jié)

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

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