【java】excel模板下载
@ApiOperation("(公用下载excel模板)")
@ApiImplicitParams({@ApiImplicitParam(name = "excelFileName", value = "文件名称", dataType = "String",required = true)})
@GetMapping("/downLoadExcelFiles")
@ResponseBody
public String downLoadExcelFiles(@RequestParam("excelFileName")String excelFileName,HttpServletResponse response){
if (excelFileName != null) {
ServletOutputStream out = null;
try {
// 以流的形式下载文件 这种方法,打成jar包之后,下载的文件,不会被损坏
InputStream fis = FileUtil.getResourcesFileInputStream("static/excelfiles/"+excelFileName+".xlsx");
if(fis!=null) {
//使用workbook方式进行下载
XSSFWorkbook workbook = new XSSFWorkbook(fis);
//处理文件名中文
response.setContentType("application/binary;charset=ISO8859-1");
String fileName = java.net.URLEncoder.encode(excelFileName, "UTF-8");
response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xlsx");
out = response.getOutputStream();
workbook.write(out);
}else{
return "该文件不存在";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
![excel模板存放地址](https://img-blog.csdnimg.cn/20200408090721210.png)
springboot excel公用模板下载
最新推荐文章于 2024-04-23 15:02:57 发布