今天做个项目,要求下载exel模板,然后填写数据,在上传,是通过poi实现的,这里给出下载excel模板的实现方案。
首先将excel模板放到resource的根目录下,然后提供下载接口,如下:
代码语言:javascript复制@ApiOperation("下载导入模板")
@GetMapping("/downloadExcel")
public void download(HttpServletResponse response) {
try {
InputStream fis = Thread.currentThread().getContextClassLoader().getResourceAsStream("批量导入模版.xlsx");
XSSFWorkbook workbook = new XSSFWorkbook(fis);
response.setContentType("application/binary;charset=ISO8859-1");
String fileName = java.net.URLEncoder.encode("批量导入模版", "UTF-8");
response.setHeader("Content-disposition", "attachment; filename=" fileName ".xlsx");
ServletOutputStream out = null;
out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件输出流
}
return;
}
然后在浏览器调用跟这个接口就可以了。