原文链接:https://blog.csdn.net/qq_32793985/article/details/115867844
之前写过一个POI 依据word模板 替换 ${xxx}属性 demo,正式上项目中,还有一些不足,比如说表格/图片/复选框等… 以poi的方式扩展起来很麻烦,还要整合/抽离一些方法,编码量太过于复杂。
(资料图片)
由此采用新的方式去实现 POI -TL
官网:Poi-tl Documentation
使用
maven
compile group: "com.deepoove", name: "poi-tl", version: "1.9.1"1使用:
模板样式:
效果:
若插入图片,则模板里应为 :${@urlImg}
@Test public void test() throws Exception { Map
// 设置年月日 datas.put("Date",DateUtils.formatDate(new Date(),"yyyy年MM月dd日")); datas.put("UserName","姜泥");
// 图片 datas.put("urlImg", Pictures.ofUrl("http://deepoove.com/images/icecream.png", PictureType.PNG) .size(300, 300).create()); // 通过模板文件直接获取 /* XWPFTemplate.compile("src/test/java/DocTemplate/testRenamed.docx").render(datas) .writeToFile("src/test/java/DocTemplate/testRenamed-print.docx");*/ // 通过字节流方式 ByteArrayOutputStream bos = new ByteArrayOutputStream(); XWPFTemplate.compile("src/test/java/DocTemplate/test.docx").render(datas).writeAndClose(bos); }1234567891011121314151617181920212223242526272829303132333435363738394041424344454647当然如果有些同学更偏爱freemarker ${}的方式,poi-tl 也支持
// 官网实例ConfigureBuilder builder = Configure.builder();XWPFTemplate.compile("template.docx", builder.buid());builder.buildGramer("${", "}");
// 我在项目中使用XWPFTemplate.compile(fileInputStream, Configure.builder().buildGramer("${", "}") .build()).render(datas).writeAndClose(bos);