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 2024-08-02 17:18  黑子菜园  阅读(317)  评论(0)    收藏  举报

导航