一、controller层,读取类路径下的模板文件,转为byte数组,写入输出流
import org.apache.poi.util.IOUtils;
@GetMapping("excelModel") public void downloadExcel(HttpServletResponse response) throws IOException { InputStream inputStream = new FileInputStream(new File(ResourceUtils.getURL("classpath:").getPath() + "/template.xlsx")); byte[] buffer = IOUtils.toByteArray(inputStream); response.setContentType("application/template.xlsx;" + "charset = UTF-8"); String name = "模板"; name = new String(name.getBytes(), "ISO-8859-1"); response.setHeader("Content-Disposition", "attachment; filename=" + name + "..xlsx"); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); toClient.write(buffer); toClient.flush(); toClient.close(); }