poi-tl(word)以及poi(excel)根据模版文件导出word、excel文件

    

@GetMapping("testWord") public void testWord(HttpServletResponse response) throws IOException { String title = "标题"; String body = "主体文字"; HashMap<String, String> map = new HashMap<>(); map.put("title",title); map.put("body",body); ConfigureBuilder builder = Configure.builder(); //builder.addPlugin('%',); XWPFTemplate render = XWPFTemplate.compile("/Users/apple/Desktop/title.docx",builder.build()).render(map); response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode("测试文件", "utf-8") +".docx"); response.setContentType("application/octet-stream;charset=UTF-8"); ServletOutputStream outputStream = response.getOutputStream(); render.write(outputStream); outputStream.flush(); render.close(); } /** * 有模版文件情况下导出excel * @param response * @throws IOException */ @GetMapping("testExcel") public void testPdf( HttpServletResponse response) { List<SysUserVo> userVos = sysUserMgr.listVo(); /*在 Java 中,try 语句后的括号是用于 try-with-resources 语句的一部分, 这是 Java 7 引入的一个特性,用于自动管理资源的关闭。 你可以在括号中声明和初始化一个或多个资源, 这些资源必须实现了 AutoCloseable 或 Closeable 接口。 当 try 代码块执行完毕后,无论是正常退出还是异常退出,这些资源都会被自动关闭。*/ try(InputStream inputStream = Files.newInputStream(Paths.get("/Users/apple/Desktop/模版文件.xlsx"))){ XSSFWorkbook workbook = new XSSFWorkbook(inputStream); XSSFSheet sheet = workbook.getSheetAt(0); int rowNum = 2; for (SysUserVo userVo : userVos) { XSSFRow row = sheet.createRow(rowNum++); for (int colum = 0; colum < 5; colum++) { XSSFCell cell = row.createCell(colum); if (colum == 0){ XSSFCellStyle style = workbook.createCellStyle(); XSSFFont font = workbook.createFont(); font.setColor(IndexedColors.BLUE.getIndex()); style.setFont(font); cell.setCellStyle(style); cell.setCellValue(userVo.getNickname()); } else if (colum == 2) { cell.setCellValue(userVo.getPhone()); } else if (colum == 3) { cell.setCellValue(userVo.getUsername()); } else if (colum == 4) { cell.setCellValue(userVo.getPassword()); } else { cell.setCellValue(userVo.getSex()); } } } response.setHeader("Content-disposition","attachment;filename=" + URLEncoder.encode("测试excel", "utf-8") +".xlsx"); response.setContentType("application/octet-stream;charset=UTF-8"); workbook.write(response.getOutputStream()); workbook.close(); response.getOutputStream().close(); }catch (Exception e){ throw new RuntimeException(e); } }