java模板下载excel相关代码

模板下载相关代码:

 public void downloadTemplate(HttpServletResponse response, Integer type) {
        String path = type == 0 ? "classpath:templates/whiteList.xlsx" : "classpath:templates/dealerWhiteList.xlsx";
        ClassPathResource resource = new ClassPathResource(path);
        try {
            InputStream inputStream = resource.getStream();
            response.reset();
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setHeader("Content-Disposition", "attachment; filename=whiteList.xlsx");
            response.setHeader("Access-Control-Allow-Origin", "*");
            ServletOutputStream outputStream = response.getOutputStream();
            byte[] bytes = new byte[1024];
            int len;
            while ((len = inputStream.read(bytes)) > 0) {
                outputStream.write(bytes, 0, len);
            }
            response.flushBuffer();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

你可能感兴趣的:(java,excel)