一,什么是FreeMarker,F(xiàn)TL模板?
FreeMarker 是一款 模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本(HTML網(wǎng)頁,電子郵件,配置文件,源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發(fā)產(chǎn)品的組件。
模板編寫為FreeMarker Template Language (FTL)。它是簡單的,專用的語言, 不是 像PHP那樣成熟的編程語言。 那就意味著要準備數(shù)據(jù)在真實編程語言中來顯示,比如數(shù)據(jù)庫查詢和業(yè)務運算, 之后模板顯示已經(jīng)準備好的數(shù)據(jù)。在模板中,你可以專注于如何展現(xiàn)數(shù)據(jù), 而在模板之外可以專注于要展示什么數(shù)據(jù)。
二,生成FTL模板文件
- 創(chuàng)建一個word文件,按照要要導出的數(shù)據(jù)進行頁面排版和布局,動態(tài)的信息可以設置為先設置為{xxx}這種格式
- 把文件另存為xml文件,然后把里面動態(tài)信息{xxx}前面加上$ , 變?yōu)?${xxx}。
- 把文件重命名為 .ftl 結尾的文件。
- 把文件放入resources/templates 目錄下
三,引入freemarker依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
四,工具類
package cn.iocoder.yudao.module.tjl.util.word;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.Map;
/**
* @ClassName: ExportWord
* @Description: 導出word工具類
* @Authror: XQD
* @Date: 2023/6/16 15:59
*/
public class ExportWord {
private Configuration configuration;
private String encoding;
private String exportPath = "D:\\data";
/**
* 構造函數(shù)
* 配置模板路徑
* @param encoding
*/
public ExportWord(String encoding) {
this.encoding = encoding;
configuration = new Configuration();
configuration.setDefaultEncoding(encoding);
configuration.setClassForTemplateLoading(this.getClass(), "/templates");
}
/**
* 導出word文檔到客戶端
* @param response
* @param fileName
* @param tplName
* @param data
* @throws Exception
*/
public void exportDoc(HttpServletResponse response, String fileName, String tplName, Map<String, Object> data, FreeMarkerConfigurer freeMarkerConfigurer) throws Exception {
response.reset();
response.setHeader("Access-Control-Allow-Origin", "*");
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName , "UTF-8"));
// 把本地文件發(fā)送給客戶端
Writer writer = response.getWriter();
Template template = getTemplate(tplName, freeMarkerConfigurer);
template.process(data, writer);
writer.close();
}
/**
* 導出word文檔到指定目錄
* @param fileName
* @param tplName
* @param data
* @throws Exception
*/
public void exportDocFile(String fileName, String tplName, Map<String, Object> data) throws Exception {
//如果目錄不存在,則創(chuàng)建目錄
File exportDirs = new File(exportPath);
if (!exportDirs.exists()) {
exportDirs.mkdirs();
}
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + fileName), encoding));
getTemplate(tplName).process(data, writer);
}
/**
* 獲取模板 打成jar包后獲取不到模板的方式 freeMarkerConfigurer
* @param name
* @return
* @throws Exception
*/
public Template getTemplate(String name, FreeMarkerConfigurer freeMarkerConfigurer) throws Exception {
freemarker.template.Configuration configuration = freeMarkerConfigurer.getConfiguration();
freemarker.template.Template template = configuration.getTemplate(name);
return template;
}
/**
* 獲取模板
* @param name
* @return
* @throws Exception
*/
public Template getTemplate(String name) throws Exception {
return configuration.getTemplate(name);
}
}
五,實例演示文章來源:http://www.zghlxwxcb.cn/news/detail-842969.html
@Autowired
FreeMarkerConfigurer freeMarkerConfigurer;
/**
* 導出word
*/
@Override
public void exportNewValvePressure1(HttpServletResponse response) throws Exception {
// TODO 獲取數(shù)據(jù)源,查詢需要動態(tài)加入的數(shù)據(jù)
// 添加表單的抬頭信息
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
String today = format.format(new Date());
String fileName = today + ".doc";
Map<String, Object> dataMap = new HashMap<>();
// 這里的key要與FTL模板中設置的${xxx}的值對應。
dataMap.put("name", "尼古拉斯");
dataMap.put("sex", "男");
dataMap.put("dizhi", "宇宙的盡頭");
dataMap.put("phone", "1666666666");
new ExportWord("UTF-8").exportDoc(response, fileName, "xinxi.ftl", dataMap, freeMarkerConfigurer);
}
六,效果圖
補充:如果導出到表格的內(nèi)容需要換行,可已在內(nèi)容中加入 “<w:br/>”文章來源地址http://www.zghlxwxcb.cn/news/detail-842969.html
到了這里,關于SpringBoot整合FreeMarker生成word表格文件(使用FTL模板)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!