word的docx模板导出,poi

本文介绍了如何在Java后台使用XWPFTemplate将存储在templates文件夹下的Word模板转化为流并响应前台请求,支持生成链接或通过`window.location.href`下载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

后台

controller使用void也行

InputStream isTemplate = this.getClass().getClassLoader().getResourceAsStream("templates/meet/testModel.docx");
String filename = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")) + "_测试表.docx";
 Map param = new HashMap();
param.put("参数key","sdf");
                    
XWPFTemplate template = XWPFTemplate.compile(isTemplate).render(param);
filename = URLEncoder.encode(filename, "UTF-8").replaceAll("\\+", "%20");
response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
OutputStream os = new BufferedOutputStream(response.getOutputStream());
 template.write(os);
os.flush();
os.close();

模板在后台存放位置

templates文件夹下中

前台写法

可以用window.location.href
或者生成a标签的方式(在复杂模板下载中提到)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值