前言:此处采用了restful的接口形式,提供导出的api,前端采用react调用。
@PostMapping(value = "exportBlankTemplate") public void exportBlankTemplate(HttpServletResponse response) { APIResult<String> result = new APIResult<>(); org.springframework.core.io.Resource resource = new ClassPathResource("template/" + EXCEL_BLANK_TEMPLATE); try { InputStream in = resource.getInputStream(); OutputStream out = response.getOutputStream(); response.reset(); response.setContentType("application/octet-stream"); response.setCharacterEncoding("utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + EXCEL_BLANK_TEMPLATE); // 循环取出流中的数据 byte[] b = new byte[200]; int len; while ((len = in.read(b)) > 0) { out.write(b, 0, len); } out.flush(); out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } }
private static final String EXCEL_BLANK_TEMPLATE = "KBJBTZS.xlsx";
excel的文件路径为:
编辑
备注:需要注意此方法的返回值,需要给void或者String 之类的,不然会报一个错误:Could not find acceptable representation
编辑
此问题解决的参考博客:springboot 下载文件 Could not find acceptable representation_AbelEthan的博客-CSDN博客