1,需要引用的maven仓库地址
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
</dependency>
如果要导出的内容包含富文本,则需要引用以下资源,来解析富文本标签
<!-- 解析富文本标签相关-->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
2,创建word模板,参考地址:Poi-tl Documentation
3,后台实现
Map<String,Object> map= new HashMap<>();
map.put("t1","trst");
String ss = <p>测试</p>
//处理带富文本的内容
map.put("t2",Jsoup.parse(ss).body().text());
List<String> files = new ArrayList<>();
String writePath = "d:/word/test" + File.separator;
File writeFile = new File(writePath);
if (!writeFile.exists()) {// 判断目录是否存在
writeFile.mkdirs();
}
XWPFTemplate template = XWPFTemplate.compile(resource.getInputStream()).render(map);
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment;filename=\""+"详情.docx");
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
map中的key值和模板中的变量需要一一对应
|