最近使用freemaker通過ftl模板生成word,這種方案較其他方式相對(duì)簡(jiǎn)便。
普通字符替換模板導(dǎo)出
1.使用${value}替換要修改個(gè)內(nèi)容,如:

2.將該word文件另存為xml格式
3.將xml文件的擴(kuò)展名直接改為ftl
4.用java代碼完成導(dǎo)出(需要導(dǎo)入freemarker.jar),其中jar包的選擇為
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
基礎(chǔ)代碼為
/** 初始化配置文件 **/
Configuration configuration = new Configuration();
/** 設(shè)置編碼 **/
configuration.setDefaultEncoding("utf-8");
/** ftl文件**/
String fileDirectory = "/home/sony/download";
/** 加載文件 **/
configuration.setDirectoryForTemplateLoading(new File(fileDirectory));
/** 加載模板 **/
Template template = configuration.getTemplate("test10.ftl");
/** 準(zhǔn)備數(shù)據(jù) **/
// Map<String,List<WordOne>> dataMap = new HashMap<>();
Map<String,Object> dataMap = new HashMap<>();
String content = "";
if(is_packed.equals("是")){
content = display_name + ",該版本采用加固技術(shù),屬于中等加固強(qiáng)度,經(jīng)過脫殼處理后,檢測(cè)" + String.valueOf(name.size()) + "項(xiàng)安全指標(biāo),共發(fā)現(xiàn)" + String.valueOf(count) + "處漏洞項(xiàng)。";
}
else
content = display_name + ",該版本未采用加固技術(shù)。";
dataMap.put("content", content);
/** 指定輸出word文件的路徑 **/
File f = new File("/home/sony/word");
if(!f.exists())
f.mkdir();
File file = new File("/home/sony/word/" + app_id);
if(!file.exists())
file.mkdir();
String filename = f.getCanonicalPath() + "/" + app_id;
// System.out.println(filename);
String outFilePath = filename + "/" + app_id + ".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();
}
普通表格模板導(dǎo)出
1.表格建立如圖:
2.xml配置文件如圖:
注意:兩個(gè)list的標(biāo)簽要把表格包住,最好寫在成對(duì)的標(biāo)簽上下。
表格循環(huán)模板導(dǎo)出
1.先看效果圖,即:
2.要完成表格的循環(huán)輸入,可以把一行看做一個(gè)類,每行中的一列為類中的一個(gè)屬性。
public class WordOne {
private String a;
private String b;
private String c;
private String d;
private String e;
private String f;
//get set以及構(gòu)造方法省略
for(int i = 0;i < 41;i++){
if(i < name.size()){
List<WordOne> wordTwo = new ArrayList<>();
String title = "無";
String titlecopy = "無";
if(status.get(i).equals("存在風(fēng)險(xiǎn)")){
title = title1.get(i);
titlecopy = title2.get(i);
}
wordTwo.add(new WordOne(name.get(i),aim.get(i),title,status.get(i),level.get(i),suggestion.get(i)));
dataMap.put("list" + String.valueOf(m),wordTwo);
m++;
}
else {
List<WordOne> wordThree = new ArrayList<>();
wordThree.add(new WordOne("0","0","0","0","0","0"));
dataMap.put("list" + String.valueOf(m),wordThree);
m++;
}
}
看起來似乎很簡(jiǎn)單,但是?。?!
吐血整理使用freemaker中遇到的坑
1.在${value}中加入的內(nèi)容不能包含<>或&等xml中使用的格式,最好將之替換或轉(zhuǎn)義,否則通過freemaker填入后,word的xml格式會(huì)識(shí)別出錯(cuò),進(jìn)而打不開word
2.添加list標(biāo)簽的時(shí)候一定要看清楚標(biāo)簽對(duì)囊括的收尾是否包含的是一個(gè)整體部分,推薦參照上圖中的添加格式(加在其他地方也不是不行,但極容易出錯(cuò))。
3.換行在網(wǎng)上搜有一個(gè)${parm}的加法,但是還是得添list。我嫌麻煩,在xml中找的了這個(gè)
<w:p w:rsidR="00CD6F2D" w:rsidRDefault="00CD6F2D" w:rsidP="0078153C"><w:pPr><w:pStyle w:val="table-items"/><w:rPr><w:rFonts w:asciiTheme="minorHAnsi" w:eastAsiaTheme="minorEastAsia" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi"/><w:bCs/><w:color w:val="auto"/><w:kern w:val="2"/><w:sz w:val="21"/><w:szCs w:val="22"/><w:bdr w:val="none" w:sz="0" w:space="0" w:color="auto"/></w:rPr></w:pPr></w:p>"
它就是xml里換行的東西= =
4.后來坑爹又讓我加超鏈接,我又瞎了眼的進(jìn)到茫茫標(biāo)簽里找,最后把這對(duì)東西提了出來
<Relationship Id="rId6" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="0308初始化IvParameterSpec函數(shù)錯(cuò)誤.txt" TargetMode="External"/>
<w:hyperlink r:id="rId6" w:history="1"><w:r w:rsidR="004335D5" w:rsidRPr="004335D5"><w:rPr><w:rStyle w:val="a8"/><w:rFonts w:asciiTheme="minorHAnsi" w:hAnsiTheme="minorHAnsi" w:cstheme="minorBidi"/><w:bCs/><w:kern w:val="2"/><w:sz w:val="21"/><w:szCs w:val="22"/><w:bdr w:val="none" w:sz="0" w:space="0" w:color="auto"/></w:rPr><w:t>Total.txt</w:t></w:r></w:hyperlink>
前一條是在word前面的配置中加的,Id="rId6"中rId不能動(dòng),6可以替換。Target的值是超鏈接的間接地址

后一條是超鏈接的標(biāo)簽,你可以在${value}中把它帖進(jìn)去,這樣就出現(xiàn)了一個(gè)顯示為Total.txt的鏈接到Target的超鏈接,其中<w:t>Total.txt</w:t>可變。