freemarker生成word模板
freemarker生成word模板
一、引入依賴
<dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.28</version> </dependency>二、工具類
注意:Configuration configuration的屬性,在某些情況下會出現(xiàn)文件文法打開的情況就是因為參數(shù)不對。
package com.dameng.util;import com.dameng.domain.FreemarkerDemo; import freemarker.cache.ClassTemplateLoader; import freemarker.core.XMLOutputFormat; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateExceptionHandler; import freemarker.template.Version; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.Locale; import java.util.Map;/*** Word文檔工具類*/ public class WordUtil {/*** 使用FreeMarker自動生成Word文檔* @param dataMap 生成Word文檔所需要的數(shù)據(jù)* @param fileName 生成Word文檔的全路徑名稱*/public static void generateWord(Map<String, Object> dataMap, String fileName) throws Exception {// 設(shè)置FreeMarker的版本和編碼格式Configuration configuration = new Configuration(Configuration.VERSION_2_3_27);configuration.setEncoding(Locale.getDefault(),"UTF-8");configuration.setDefaultEncoding("UTF-8");configuration.setOutputFormat(XMLOutputFormat.INSTANCE);//設(shè)置異常處理器 這樣的話 即使沒有屬性也不會出錯 如:${list.name}...不會報錯configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER); // // 設(shè)置FreeMarker生成Word文檔所需要的模板的路徑configuration.setTemplateLoader(new ClassTemplateLoader(FreemarkerDemo.class,"/templates/"));// 設(shè)置FreeMarker生成Word文檔所需要的模板///Template t = configuration.getTemplate("check_freework202202262238.ftl");Template t = configuration.getTemplate("check_freework202203061.ftl");// 創(chuàng)建一個Word文檔的輸出流Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(fileName)), "UTF-8"));//FreeMarker使用Word模板和數(shù)據(jù)生成Word文檔t.process(dataMap, out);out.flush();out.close();}}三、實體類
package com.dameng.domain;import freemarker.cache.ClassTemplateLoader; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException;import java.io.IOException; import java.io.StringWriter; import java.nio.charset.Charset; import java.util.HashMap; import java.util.Map;public class FreemarkerDemo {public static void main(String[] args) throws IOException, TemplateException {/*初始化freemarker模板*/Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);configuration.setDefaultEncoding(Charset.forName("UTF-8").name());//這里的ClassTemplateLoader 的basePackagePath 如果是 / 開頭就是絕對路徑。如果不是/ 開頭就是相對于//com.saoft.fastdemo.gen.FreemarkerDemo.java 這個類的相對路徑,會以com.saoft.fastdemo.gen這個目錄開始找//FileTemplateLoaderconfiguration.setTemplateLoader(new ClassTemplateLoader(FreemarkerDemo.class,"/templates/"));//獲取模板Template template = configuration.getTemplate("ssm/demo.ftl");//需要注入的數(shù)據(jù)Map<String, Object> dataMap = new HashMap<>();dataMap.put("testKey", "This is key");//輸出位置 這里用字符串輸出,如果要輸出文件自行替換StringWriter writer = new StringWriter();template.process(dataMap, writer);System.out.println(writer.getBuffer());} }四、main方法
public static void main(String[] args) {Map<String, Object> wordDataMap = getWordData(dataSource,systemInfo);//輸出概述的信息wordDataMap.put("systemInfo",systemInfo);wordDataMap.put("dbSummaryInfo",dbSummaryInfo);WordUtil.generateWord(wordDataMap,generateFileName);}注意:FreemarkerDemo 類有指定模板的位置。
五、docx文檔的選項
六、踩坑
6.1 特殊符號
使用FreeMarker模板生成Word文檔時,如果填充的數(shù)據(jù)字符串中含有特殊字符< 、>、&,那么生成的Word文檔是無法打開的。因為這些字符在生成Word文檔時被認為是FreeMarker模板的標簽,如果這些字符不經(jīng)過處理就直接用于生成Word文檔,使用Word打開生成的文檔就會報錯,但以xml的方式打開,卻會發(fā)現(xiàn)所有內(nèi)容都是完整的,唯獨上面三個特殊字符出問題。因此,在處理數(shù)據(jù)時需要對這三個特殊字符進行處理。
6.2 換行符
使用FreeMarker模板生成Word文檔時,如果填充的數(shù)據(jù)字符串過長且當中使用"\n"進行換行,則生成的Word文檔中并沒有起到換行的作用。需要先將"\n"全部替換成"<w:p></w:p>",然后使用替換后的字符串數(shù)據(jù)生成Word文檔,才能達到換行的效果。
6.3 內(nèi)容是xml格式
使用本文方法生成的Word文檔的內(nèi)容實質(zhì)上是xml格式的,因此,生成的Word文檔即可以使用Word打開,也可以使用xml文檔工具打開。如果使用Java程序去讀取生成的Word文檔的內(nèi)容,則讀出來的也是xml格式的內(nèi)容。如果想要將內(nèi)容轉(zhuǎn)換成Word格式,則可以使用Word打開生成的文檔然后另存為Word文檔格式。
6.4 后綴是.doc
使用本文方法生成的Word文檔的后綴必須是.doc格式,而不能是.docx格式,否則生成的Word文檔無法打開。如果想要將后綴轉(zhuǎn)換成.docx格式,則可以使用Word打開生成的文檔然后另存為Word文檔格式。
6.5生成xml打開加密格式
這是因為notepad++缺少xml的插件,所以打開就是加密的。使用記事本打開或者放到IDEA中打開
資料
https://blog.csdn.net/weixin_44516305/article/details/88049964
https://www.cnblogs.com/zwqh/p/7182953.html
總結(jié)
以上是生活随笔為你收集整理的freemarker生成word模板的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Hibernate框架整合DM数据库
- 下一篇: java连接linux服务器执行shel