EasyExcel 模板文件下载
时间: 2023-08-19 14:17:52 浏览: 65
你可以使用以下代码来实现 EasyExcel 模板文件下载:
```java
@GetMapping("/downloadTemplate")
public void downloadTemplate(HttpServletResponse response) throws IOException {
// 模板文件路径
String templatePath = "classpath:templates/user_template.xlsx";
// 设置响应头
response.setHeader("Content-Disposition", "attachment;filename=user_template.xlsx");
response.setContentType("application/octet-stream;charset=UTF-8");
// 下载模板文件
InputStream inputStream = new ClassPathResource(templatePath).getInputStream();
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
IOUtils.closeQuietly(inputStream);
}
```
在上述代码中,我们首先指定了模板文件的路径,这里以 `classpath:templates/user_template.xlsx` 为例。然后,我们设置了响应头,告诉浏览器需要下载文件,并指定文件名。最后,我们使用 IOUtils 将模板文件的内容复制到响应输出流中,即可实现模板文件的下载。