https://gitee.com/coder-msc/my-world
将项目跑起来,直接浏览器访问即可下载
http://127.0.0.1:8080/download/word/test
<!--world工具-->
<dependency>
<groupId>com.deepoove</groupId>
<artifactId>poi-tl</artifactId>
<version>1.12.0</version>
</dependency>
@RestController
public class OutputWolrd {
/**
* 获取报告
*/
@RequestMapping("/download/word/test")
public void downloadWord(HttpServletResponse response) throws Exception {
ClassPathResource resource = new ClassPathResource("template.docx");
InputStream inputStream = resource.getInputStream();
XWPFTemplate template = XWPFTemplate.compile(inputStream).render(
new HashMap() {{
put("ID", "ZHANG_SAN_091");
put("NO", "KB.6890451");
put("taitou", "深圳XX家装有限公司");
put("consignee", "丙丁");
}});
// 文件流传输
// template.writeAndClose(new FileOutputStream("output.docx"));
// 使用网络流进行传输
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("付款通知书.docx", "UTF-8"));
// HttpServletResponse response
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, bos, out);
}
}
http://deepoove.com/poi-tl/#