[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;
}

模板存放位置:

 

 

 
       


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM