Poi-tl word模版导出
pom文件
<dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> <version>1.10.6</version> <exclusions> <exclusion> <artifactId>xml-apis</artifactId> <groupId>xml-apis</groupId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>easyexcel</artifactId> <version>3.3.4</version> <exclusions> <exclusion> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> </exclusion> <exclusion> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> </exclusion> <exclusion> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> </exclusion> </exclusions> </dependency>
使用时需要注意排除jar冲突,否则会报错,目前使用的都是4.1.2版本的
模板放在resource下的template目录下
模版内容,填充的参数放在 {{}} 中,参考链接 http://deepoove.com/poi-tl/
部分代码实现,返回流提供前端下载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | @Override public void wordExport(HttpServletResponse response, SewageSafeContinueImprove sewageSafeContinueImprove) throws IOException { response.setContentType( "application/octet-stream" ); response.setHeader( "Content-disposition" , "attachment;filename=\"" + "out_template.docx" + "\"" ); String safeContinueImproveId = sewageSafeContinueImprove.getSafeContinueImproveId(); SewageSafeContinueImprove continueImprove = this .getById(safeContinueImproveId); Map<String, Object> beanToMap = BeanUtil.beanToMap(continueImprove); beanToMap.put( "date" , DateUtil.format( new Date(), "yyyy年MM月dd日" )); ClassPathResource res = new ClassPathResource( "template/safe.docx" ); InputStream is = res.getInputStream(); XWPFTemplate template = XWPFTemplate.compile( is ).render(beanToMap); // template.writeToFile("D:\\桌面\\sssssssssssss.docx"); OutputStream out = response.getOutputStream(); BufferedOutputStream bos = new BufferedOutputStream( out ); template.write(bos); bos.flush(); out .flush(); //关闭流 PoitlIOUtils.closeQuietlyMulti(template, bos, out ); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
2020-07-30 @RequestBody Content type 'multipart/form-data;boundary=----WebKitFormBoundarybEyHr0FZTTOHW7Vq;charset=UTF-8' not supported