開(kāi)心一笑
感冒了很難受劫拗,她悶在被窩里給男朋友發(fā)短信"我感冒了..."并決定如果對(duì)方回答"多喝點(diǎn)水"就一腳踹了他。過(guò)一會(huì)兒手機(jī)振動(dòng)起來(lái),短信內(nèi)容:"開(kāi)門唉侄。"......這個(gè)大笨蛋悠垛!誰(shuí)讓他來(lái)的啦线定!她起身用最快的速度沖去門口,此時(shí)手機(jī)再次振動(dòng)确买,她一手開(kāi)門一手興奮的點(diǎn)開(kāi):"多呼吸點(diǎn)新鮮空氣斤讥,運(yùn)動(dòng)運(yùn)動(dòng)。
提出問(wèn)題
Java中如何利用FreeMarker導(dǎo)出word文檔湾趾?芭商??
解決問(wèn)題
1.先用word準(zhǔn)備一個(gè)模板撑帖,如下圖:
2.我們把word文檔另存為xml格式的文件蓉坎,用Notepad++工具打開(kāi),一下只截取部分內(nèi)容.
3.我們開(kāi)始處理第一個(gè)問(wèn)題:文字處理胡嘿,Ctrl+F找到XML文檔中文字蛉艾,將“以下省略一萬(wàn)字”替換為${textDeal},保存文件衷敌,將文件的后綴改為.ftl勿侯,自此模板制作成功。
4.編碼實(shí)現(xiàn)文本替換:
package com.hwy.test;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* word導(dǎo)出
* Created by Ay on 2016/6/27.
*/
public class WordDocExportTest {
public static void main(String[] args) throws Exception{
/** 初始化配置文件 **/
Configuration configuration = new Configuration();
/** 設(shè)置編碼 **/
configuration.setDefaultEncoding("utf-8");
/** 我的ftl文件是放在D盤(pán)的**/
String fileDirectory = "D:\\";
/** 加載文件 **/
configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
/** 加載模板 **/
Template template = configuration.getTemplate("FreeMarker中word導(dǎo)出XML.ftl");
/** 準(zhǔn)備數(shù)據(jù) **/
Map<String,String> dataMap = new HashMap<>();
/** 在ftl文件中有${textDeal}這個(gè)標(biāo)簽**/
dataMap.put("textDeal","一下省略一萬(wàn)字");
/** 指定輸出word文件的路徑 **/
String outFilePath = "D:\\myFreeMarker.doc";
File docFile = new File(outFilePath);
FileOutputStream fos = new FileOutputStream(docFile);
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
template.process(dataMap,out);
if(out != null){
out.close();
}
}
}
5.編碼實(shí)現(xiàn)圖片的替換
具體代碼如下:
package com.hwy.test;
import freemarker.template.Configuration;
import freemarker.template.Template;
import sun.misc.BASE64Encoder;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* word導(dǎo)出
* Created by Ay on 2016/6/27.
*/
public class WordDocExportTest {
public static void main(String[] args) throws Exception{
/** 初始化配置文件 **/
Configuration configuration = new Configuration();
/** 設(shè)置編碼 **/
configuration.setDefaultEncoding("utf-8");
/** 我的ftl文件是放在D盤(pán)的**/
String fileDirectory = "D:\\";
/** 加載文件 **/
configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
/** 加載模板 **/
Template template = configuration.getTemplate("FreeMarker中word導(dǎo)出XML.ftl");
/** 準(zhǔn)備數(shù)據(jù) **/
Map<String,String> dataMap = new HashMap<>();
/** 圖片路徑 **/
String imagePath = "D:\\apple.jpg";
/** 將圖片轉(zhuǎn)化為**/
InputStream in = null;
byte[] data = null;
try {
in = new FileInputStream(imagePath);
data = new byte[in.available()];
in.read(data);
in.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(in != null){
in.close();
}
}
/** 進(jìn)行base64位編碼 **/
BASE64Encoder encoder = new BASE64Encoder();
/** 在ftl文件中有${textDeal}這個(gè)標(biāo)簽**/
dataMap.put("textDeal","一下省略一萬(wàn)字");
/** 圖片數(shù)據(jù)**/
dataMap.put("myImage",encoder.encode(data));
/** 指定輸出word文件的路徑 **/
String outFilePath = "D:\\myFreeMarker.doc";
File docFile = new File(outFilePath);
FileOutputStream fos = new FileOutputStream(docFile);
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
template.process(dataMap,out);
if(out != null){
out.close();
}
}
}
結(jié)果缴罗,I love you這張圖片又出現(xiàn)了:
6.freeMarker表格處理
1)同樣道理助琐,找到表格的位置,替換成freeMarker標(biāo)簽面氓,圖片如下:
具體代碼:
package com.hwy.test;
import freemarker.template.Configuration;
import freemarker.template.Template;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* word導(dǎo)出
* Created by Ay on 2016/6/27.
*/
public class WordDocExportTest {
public static void main(String[] args) throws Exception{
/** 初始化配置文件 **/
Configuration configuration = new Configuration();
/** 設(shè)置編碼 **/
configuration.setDefaultEncoding("utf-8");
/** 我的ftl文件是放在D盤(pán)的**/
String fileDirectory = "D:\\";
/** 加載文件 **/
configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
/** 加載模板 **/
Template template = configuration.getTemplate("FreeMarker中word導(dǎo)出XML.ftl");
/** 準(zhǔn)備數(shù)據(jù) **/
Map<String,List<Student>> dataMap = new HashMap<>();
/** 表格數(shù)據(jù)初始化 **/
List<Student> studentList = new ArrayList<>();
studentList.add(new Student("100424060","小毅","男","25"));
studentList.add(new Student("100424030","小蘭","女","25"));
/** 表格數(shù)據(jù) studentList和freemarker標(biāo)簽要對(duì)應(yīng)**/
dataMap.put("studentList",studentList);
/** 指定輸出word文件的路徑 **/
String outFilePath = "D:\\myFreeMarker.doc";
File docFile = new File(outFilePath);
FileOutputStream fos = new FileOutputStream(docFile);
Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240);
template.process(dataMap,out);
if(out != null){
out.close();
}
}
}
Student類如下:這里有一個(gè)問(wèn)題兵钮,如果Student類寫(xiě)在WordDocExportTest類中,就會(huì)出現(xiàn)問(wèn)題舌界,很奇怪芙扎,只能單獨(dú)另啟一個(gè)類了墩莫。
package com.hwy.test;
/**
* Created by Ay on 2016/6/29.
*/
public class Student{
private String id;
private String name;
private String sex;
private String age;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public Student(String id, String name, String sex, String age) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
}
}
結(jié)果如下:

7.word中字體樣式調(diào)整
1)我們把四種的字體“這里是字體樣式......”在word中字體樣式改為仿宋_gb2312,大小改為30號(hào)如下:
2)用同樣的方法,把word轉(zhuǎn)變?yōu)?strong>ftl文件,打開(kāi)如下
所有以后word需要什么的樣式召耘,都是在原先word調(diào)好樣式,在轉(zhuǎn)變?yōu)閒tl文件即可
8.頁(yè)眉頁(yè)腳設(shè)置
1)相同道理,在原先word插入頁(yè)眉“Hello Freemarker”,另存為xml文件垃喊,再后綴為ftl文件即可,既是我們要的文件
讀書(shū)感悟
來(lái)自《我腦海中的橡皮擦》
- 我們破產(chǎn)了袜炕!沒(méi)關(guān)系本谜,我來(lái)付錢。
- 不要困擾于過(guò)去妇蛀,時(shí)間會(huì)治愈一切耕突。
- 要想原諒一個(gè)人,就是要把心里的某個(gè)地方留給他评架。