模板
feeDate:要填充的单个的值
name:是个集合
封装的公共方法
/** * * EasyExcel 填充报表 * * @param response * @param list 填充集合 * @param map 填充单个的值 * @param sheetNo 填充到哪个Sheet页 Index of sheet, 0 base. * @param filename 文件名 * @param inputStream 文件流. */ public void fillReportWithEasyExcel(HttpServletResponse response, Integer sheetNo, List<?> list, Map<String, String> map, String filename, InputStream inputStream){ ExcelWriter excelWriter = null; try { OutputStream outputStream = response.getOutputStream(); response.setHeader("Content-disposition", "attachment; filename=" + filename); response.setContentType("application/msexcel;charset=UTF-8");//设置类型 response.setHeader("Pragma", "No-cache");//设置头 response.setHeader("Cache-Control", "no-cache");//设置头 response.setDateHeader("Expires", 0);//设置日期头 excelWriter = EasyExcel.write(outputStream).withTemplate(inputStream).build(); WriteSheet writeSheet = EasyExcel.writerSheet(sheetNo).build(); FillConfig fillConfig = FillConfig.builder().forceNewRow(Boolean.TRUE).build(); excelWriter.fill(list, fillConfig, writeSheet); excelWriter.fill(map, writeSheet); }catch (Exception e){ e.printStackTrace(); }finally { excelWriter.finish(); try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } }
业务代码调用例子:
//数据库里查询得到的list集合 List<XXX> list = mapper.select(); //要填充的单个的值 Map<String, String> map = new HashMap<>(); map.put("feeDate", feeDate); map.put("name", name); String filename = "filename"; //工程路径下有个模板文件 String modelPath = "classpath:static/XXX/xxxxxx.xlsx"; //读取模板文件 InputStream inputStream = ResourceUtils.getURL(modelPath).openStream(); //调用方法即可 fillReportWithEasyExcel(response, 0, list, map, filename, inputStream);