springboot resources 目录下excel文件下载
·
@GetMapping(value = "downLoadExcel")
public void downLoadExcel() {
InputStream fis = null;
ServletOutputStream sos = null;
ByteArrayOutputStream os = new ByteArrayOutputStream();
try {
Workbook workbookTwo = new XSSFWorkbook(getClass().getClassLoader().getResourceAsStream("importExcel.xlsx"));
sos = response.getOutputStream();
workbookTwo.write(os);
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
response.setCharacterEncoding("utf-8");
// 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
String fileName = URLEncoder.encode("test.xlsx", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
sos.write(os.toByteArray());
sos.flush();
} catch (Exception e) {
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (os != null) {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (sos != null) {
try {
sos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)