目錄
一 FreeMarker簡(jiǎn)介
二 集成springboot,實(shí)現(xiàn)案例導(dǎo)出
三 常見面試題總結(jié)
一 FreeMarker簡(jiǎn)介
FreeMarker?是一款 模板引擎: 即一種基于模板和要改變的數(shù)據(jù), 并用來生成輸出文本(HTML網(wǎng)頁(yè),電子郵件,配置文件,源代碼等)的通用工具。 是一個(gè)Java類庫(kù)。
二 集成springboot,實(shí)現(xiàn)案例導(dǎo)出
在本地磁盤隨便準(zhǔn)備一個(gè)文件,內(nèi)容體如下:
內(nèi)容案例如下:
?代碼實(shí)現(xiàn):
?2.1 導(dǎo)入jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2.2 新建FileController.java類,代碼實(shí)現(xiàn)如下:
package com.yty.system.controller;
import com.alibaba.fastjson.JSONObject;
import freemarker.template.Configuration;
import freemarker.template.Template;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@RestController
@RequestMapping("/file")
@Api(tags = "文件管理api")
public class FileController {
@GetMapping("exportPDF")
@ApiOperation(value = "文件導(dǎo)出到PDF",notes = "文件導(dǎo)出到PDF")
public void exportPDF(HttpServletResponse response) throws Exception{
try {
exportWord(response, "2012001.ftl", "/templates/ftl/");
}catch (Exception e) {
e.printStackTrace();
}
}
public void exportWord(HttpServletResponse response, String templateName, String templatePath) throws Exception{
Configuration configuration = new Configuration(Configuration.getVersion()); // 創(chuàng)建一個(gè)Configuration對(duì)象
configuration.setClassForTemplateLoading(this.getClass(), templatePath);
configuration.setDefaultEncoding("utf-8");
//必須加此參數(shù),否則任意key的值為空freemark都會(huì)報(bào)錯(cuò)
configuration.setClassicCompatible(true);
// 選擇模板
Template template = configuration.getTemplate(templateName); //加載模板
// 導(dǎo)出文件名
String fileName = System.currentTimeMillis() + ".doc";
// 導(dǎo)出文件路徑
String path = "D:\\system\\ee\\模板\\" + fileName;
// 創(chuàng)建文件
File file = new File(path);
Writer out = new FileWriter(file);
// 填充數(shù)據(jù)
JSONObject jsonObject = new JSONObject();
jsonObject.put("surveyPersonName", "孫悟空");
jsonObject.put("createTime", "2012-09");
//調(diào)用模板對(duì)象的process方法輸出文件
template.process(jsonObject, out);
// 下載文件
downloadFile(response, file, out);
}
public void downloadFile(HttpServletResponse response, File file, Writer out) throws Exception{
// 下載文件
byte[] buffer = new byte[1024];
response.addHeader("Content-Disposition","attachment;filename=" + new String(file.getName().getBytes(), "ISO-8859-1"));
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
// 關(guān)閉流
os.close();
bis.close();
fis.close();
out.close();
file.delete();
}
}
直接復(fù)制代碼,運(yùn)行結(jié)果:
?集成完畢,數(shù)據(jù)已填充,導(dǎo)出完畢文章來源:http://www.zghlxwxcb.cn/news/detail-689699.html
三 常見面試題總結(jié)
待補(bǔ)充...............文章來源地址http://www.zghlxwxcb.cn/news/detail-689699.html
到了這里,關(guān)于freemarker學(xué)習(xí)+集成springboot+導(dǎo)出word的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!