前言
poi-tl 是一個(gè)基于 Apache POI 和 FreeMarker 的 Java 模板引擎,可以用于動態(tài)生成 Word、Excel、PowerPoint 等文檔。使用 poi-tl 可以方便地將數(shù)據(jù)填充到模板中,生成符合要求的文檔。
poi-tl官方文檔地址:http://deepoove.com/poi-tl/#_%E7%89%88%E6%9C%AC
先附上導(dǎo)出效果圖:
1.創(chuàng)建word模板,并將模板放在resource文件夾下
這步建議手動創(chuàng)建,之前客服提供了一個(gè)pdf文件,我把它轉(zhuǎn)成了word,用這個(gè)word當(dāng)模板.結(jié)果意外發(fā)生了,動態(tài)表格數(shù)據(jù)不會自動分頁,所有數(shù)據(jù)都擠在第一頁…改模板,加分頁符…都不好用,最后手動新建一個(gè)模板就好了…
數(shù)據(jù)結(jié)構(gòu):
代碼中可以用對象封裝,也可以用map.
區(qū)塊對是 {{?innerList}} {{/innerList}},我想要的樣式就是循環(huán)這部分內(nèi)容
{{contestExcelList}} 是動態(tài)列表集合
外層out(對象) :date (字符串) innerList(集合)
inner 對象:
title1(字符串)
title2(字符串)
@image(圖片)
contestExcelList(集合)-
contestExcel(對象):num(字符串)
projectName(字符串)
score(字符串)
2.引入依賴
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.10.0</version>
</dependency>
<!--word 轉(zhuǎn)pdf 注意依賴是否需要排除,以免沖突-->
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-MOXy</artifactId>
<version>8.2.9</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.2.9</version>
</dependency>
3.代碼實(shí)現(xiàn)
public void downLoad(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<Map<String, Object>> innerMapList= new ArrayList<>(); //inner集合
Map<String, Object> innerMap = new HashMap<>(); //inner對象
List<ContestExcel> contestExcelList= new ArrayList<>(); //自己的動態(tài)表格數(shù)據(jù)
for (int j = 0; j < 10; j++) {
ContestExcel contestExcelEntity = new ContestExcel();
contestExcelEntity.setNum(j+1);
contestExcelEntity.setProjectName("項(xiàng)目名稱");
contestExcelEntity.setScore("99");
contestExcelList.add(contestExcelEntity);
}
innerMap .put("title1", Texts.of("標(biāo)題1").create());
innerMap .put("title2", Texts.of("標(biāo)題2").create());
innerMap .put("image", Pictures.ofUrl("圖片url鏈接");
innerMap .put("contestExcelList", contestExcelList);
innerMapList.add(innerMap);
Map<String, Object> outMap= new HashMap<>(); //外層out(對象)
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
outMap.put("innerList", innerMapList);
outMap.put("date", sdf.format(date));
String name = "打分表";
String destFilePath = TestFileUtil.getPath();
//動態(tài)表格變量可以用[] 對應(yīng)
LoopRowTableRenderPolicy policy = new LoopRowTableRenderPolicy();
//這里可以指定一個(gè)config類,用來指定一些規(guī)則,也可以改變模板中{{}}的這種格式
Configure config = Configure.builder()
.bind("contestExcelEntityList", policy).build();
//獲取模板
InputStream inputStream = ClassUtils
.getDefaultClassLoader()
.getResourceAsStream("wordTemplate.docx"); //將模板放在resource文件夾下
//生成docx文件
XWPFTemplate compile = XWPFTemplate.compile(inputStream, config);
compile.render(map);
compile.writeToFile(destFilePath + name + ".docx");
inputStream.close();
//轉(zhuǎn)成pdf文件并導(dǎo)出
outputMethod(response, toPdf2(destFilePath, name));
轉(zhuǎn)pdf 和導(dǎo)出方法:文章來源:http://www.zghlxwxcb.cn/news/detail-810512.html
//轉(zhuǎn)pdf方法
public static String toPdf2(String destFilePath, String zipName) throws Exception {
String destFilePath2 = "";
char firstChar = destFilePath.charAt(0);
if (firstChar == '/') {
destFilePath2 = destFilePath.substring(1);
} else {
destFilePath2 = destFilePath;
}
//此處代碼 參考 https://editor.csdn.net/md/?articleId=133790383
WordToPdfTest_Docx4j.wordToPdf(destFilePath2 + zipName + ".docx", destFilePath2 + zipName + ".pdf");
return destFilePath2 + zipName + ".pdf";
}
}
//導(dǎo)出方法
public void outputMethod(HttpServletResponse response, String filePath) {
ServletOutputStream out = null;
// File file = new File("d:\\test\\result.pdf");
File file = new File(filePath);
FileInputStream inputStream = null;
try {
out = response.getOutputStream();
inputStream = new FileInputStream(file);
if (filePath.contains(".xlsx")) {
//
/** 導(dǎo)出excel文件流 */
response.setHeader("content-Type", "application/vnd.ms-excel");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
response.setCharacterEncoding("UTF-8");
} else {
/** 導(dǎo)出pdf文件流 */
response.setCharacterEncoding("UTF-8");
response.setContentType("application/pdf");
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline; filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
}
// 讀取文件流
int len = 0;
byte[] buffer = new byte[1024 * 10];
while ((len = inputStream.read(buffer)) != -1) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
logger.error("輸入流關(guān)閉失敗, {}", e);
}
try {
out.close();
} catch (IOException e) {
logger.error("輸出流關(guān)閉失敗, {}", e);
}
}
file.delete();//刪除.xlsx 或 pdf文件
if (filePath.contains(".pdf")) { //如果是pdf ,還需要刪除生成的docx
File fileDocx = new File(filePath.substring(0, filePath.lastIndexOf(".")) + ".docx");
fileDocx.delete();
}
}
4.前端blob導(dǎo)出參考:
https://editor.csdn.net/md/?articleId=133790129文章來源地址http://www.zghlxwxcb.cn/news/detail-810512.html
到了這里,關(guān)于【Java】poi-tl 使用Word模板渲染動態(tài)表格的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!