JAVA 导出本地项目Excel模板

JAVA 导出本地项目Excel模板

模板位置 实现代码

@ApiOperation("模板下载API")
    @PostMapping("/importTemplate")
    public void exportExcel(HttpServletResponse response){
          
   
        String filePath = "./templates/BOM.xlsx";
        OutputStream out = null;
        InputStream in = null;
        XSSFWorkbook workbook = null;
        try {
          
   
            out = response.getOutputStream();
            in = new BufferedInputStream(new ClassPathResource(filePath).getInputStream());
            String type = new MimetypesFileTypeMap().getContentType("BOM.xlsx");
            response.setContentType(type);
            String name = URLEncoder.encode("物料信息导入模板.xlsx", "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename=" + name);
            workbook = new XSSFWorkbook(in);
            workbook.write(out);
        } catch (Exception e) {
          
   
            e.printStackTrace();
        }finally {
          
   
            IoUtil.close(workbook);
            IoUtil.close(out);
            IoUtil.close(in);
        }

    }
经验分享 程序员 微信小程序 职场和发展