poi-tl导出word文档

1、依赖:

 

2、参考博文:

https://blog.csdn.net/qq_31970227/article/details/113246795

https://www.cnblogs.com/pengdai/p/16537534.html#template%E6%A8%A1%E6%9D%BF

3、主要实现代码:

String filename = “导出文件的名字.docx”;

httpServletResponse response.setContentType("application/msword);

response.setcharacterEncoding("utf-8");

String name = URLEncoder.encode(filename,UTF-8);

response.setHeader(HttpHeaders.CONTENT_DISPISITION,"attachment; filename=\""+name+"\"; filename*=utf-8"+name);

InputStream fis = this.getClass().getClassLoader.getResourceAsStream(“templates/”+filename);

if(fis ==null){

  throw exception

}

LoopRowTableRenderPolicy rowPolicy = new LoopRowTableRenderPolicy();

Configure config = Configure.builder().bind("fishPlist",rowPolicy)    // 此处绑定数据到模版中的表格中去渲染,如果有多个列表时需加.bind("list",policy)继续绑定新列表即可

.build();

XWPFTemplatetemp = XWPFTemplatetemp.compile(fis,config).render(data);  // data为数据实体

ByteArrayOutputStream out = new ByteArrayOutputStream();

temp.write(out);

if(fis != null){fis.close();}

InputStream docx = new ByteArrayInputStream(out.toByteArray());

hutool.IoUtil.copy(docx,response.getOutputStream());

out.close();

 

posted on   黑子菜园  阅读(158)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· 上周热点回顾(2.17-2.23)
· 如何使用 Uni-app 实现视频聊天(源码,支持安卓、iOS)
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

统计

点击右上角即可分享
微信分享提示