前言:此处采用了restful的接口形式,提供导出的api,前端采用react调用。
@PostMapping(value = "exportBlankTemplate") public void exportBlankTemplate(HttpServletResponse response) { APIResultresult = 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博客
Copyright © 2023 leiyu.cn. All Rights Reserved. 磊宇云计算 版权所有 许可证编号:B1-20233142/B2-20230630 山东磊宇云计算有限公司 鲁ICP备2020045424号
磊宇云计算致力于以最 “绿色节能” 的方式,让每一位上云的客户成为全球绿色节能和降低碳排放的贡献者