POI的使用及导出excel报表首先,了解poi是什么?一、基本概念 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。二、基本结构 HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel OOXML格式档案的功能。 HWPF - 提供读写Microsoft Word格式档案的功能。 HSLF - 提供读写Microsoft PowerPoint格式档案的功能。 HDGF - 提供读写Microsoft Visio格式档案的功能。概念先了解这么多,具体可自行谷歌一开始模仿公司项目的代码,使用一些工具类ExcelHelper、ExportUtil 来创建excel,并导出,部分代码如下:// …ExcelHelper.writeExcel(fileName, excelVOList, ProblemLiveVO.class, null, titles);ExportUtil.exportToClient(response, ContentType.MULTIPART_FORM_DATA.toString(), ParamsUtil.EXCEL, fileName, true); 后来发现,用此方式不太合适,一般我们要做报表的话,产品经理会先给一个报表模板,我们可以将此报表模板做得美观一些(设置好表头颜色等),然后先用poi读取这份模板,再添加修改,最后直接导出到客户端。这样效率也更高。其实poi的使用是比较简单的,可参考以下代码:(有详细注释)@Transactional(propagation = Propagation.REQUIRED, readOnly = true)public ResponseEntity<byte[]> downloadExcel() { // 1.封装数据到 List(此处需按实际情况封装数据) List<ReportFinancial> list = reportFinancialDao.findAll(); try { // 2.读取报表模板 URL resource = this.getClass().getClassLoader() .getResource(“excel/NiceReportForm.xlsx”); Assert.notNull(resource, “读取不到财务报表模板”); String path = resource.getPath(); FileInputStream fis = new FileInputStream(path); String fileName = “财务报表” + new SimpleDateFormat(“yyyyMMddHHmmss”) .format(new Date()) + “.xlsx”; // 创建Workbook XSSFWorkbook workbook = new XSSFWorkbook(fis); // 读取sheet1 XSSFSheet sheet = workbook.getSheetAt(0); // 设置表格居中 XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setAlignment(HorizontalAlignment.CENTER); // 3.向excel中添加数据 int rowLine = 2; // 2即为第三行, 从0开始算, 根据实际情况设置 for (ReportFinancial report : list) { XSSFRow row = sheet.createRow(rowLine++); row.createCell(0).setCellValue(report.getReportFinancialId()); row.createCell(1).setCellValue(report.getNickName()); row.createCell(2).setCellValue(report.getMoney()); row.createCell(3).setCellValue(report.getCreateTime()); // …根据实际数据添加 // 设置以上添加的表格数据居中 for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) { row.getCell(i).setCellStyle(cellStyle); } } // 4.将workbook中的数据写到输出流中 ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); baos.close(); // 5.设置请求头, 返回一个ResponseEntity HttpHeaders headers = new HttpHeaders(); headers.set(HttpHeaders.CONTENT_ENCODING, “UTF-8”); headers.setContentDispositionFormData(“attachment”, fileName); headers.setPragma(fileName); headers.setCacheControl(“No-cache”); headers.setDate(“Expires”, 0); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<>(baos.toByteArray(), headers, HttpStatus.CREATED); } catch (Exception e) { e.printStackTrace(); } return null;}此部分代码为Service层的代码,返回值为ResponseEntity<byte[]>,Controller层直接将ResponseEntity返回就可以了,前端访问此接口就能下载excel文件。此 downloadExcel 方法可作为参考,需要更改的有:第一步,根据实际情况封装数据第二步,模板报表的位置,我是放在resources/excel下第三步,将实际数据添加到表格的某行某列中 row.createCell(0).setCellValue( )当然,将数据写回客户端,还有另一种方式,写到 HttpServletResponse response 的输出流,可自行选择。最后,此篇文章只是实现简单的excel表格,需要更加具体详细的设置,可自行查看API广州芦苇科技Java开发团队芦苇科技-广州专业互联网软件服务公司抓住每一处细节 ,创造每一个美好关注我们的公众号,了解更多想和我们一起奋斗吗?lagou搜索“ 芦苇科技 ”或者投放简历到 server@talkmoney.cn 加入我们吧关注我们,你的评论和点赞对我们最大的支持