欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > Apache POl的使用(导出报表)

Apache POl的使用(导出报表)

2024/10/24 21:20:31 来源:https://blog.csdn.net/a2547724092/article/details/141819267  浏览:    关键词:Apache POl的使用(导出报表)

介绍

Apache POl是一个处理Miscrosoft Office各种文件格式的开源项目。简单来说就是,我们可以使用 PO! 在 Java 程序中对Miscrosoft Office各种文件进行读写操作。一般情况下,POI都是用于操作 Excel 文件。

Apache POl的应用场景:

  • 银行网银系统导出交易明细
  • 各种业务系统导出Excel报表
  • 批量导入业务数据

 导入maven坐标

   <!-- poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>${poi}</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>${poi}</version></dependency>

写操作 

    /*** 通过POI创建Excle文件*/@Testpublic void write() throws Exception{//在内存中创建一个excle文件XSSFWorkbook excel=new XSSFWorkbook();//在excel文件中创建一个sheet页XSSFSheet sheet = excel.createSheet("info");//创建行对象,rownum编号从0开始XSSFRow row = sheet.createRow(1);//在行上创建单元格row.createCell(1).setCellValue("姓名");row.createCell(2).setCellValue("宁波");//创建一个新行row=  sheet.createRow(2);//在行上创建单元格row.createCell(1).setCellValue("张三");row.createCell(2).setCellValue("上海");//创建一个新行row=  sheet.createRow(3);//在行上创建单元格row.createCell(1).setCellValue("李四");row.createCell(2).setCellValue("杭州");FileOutputStream out= new FileOutputStream(new File("D:\\BaiduNetdiskDownload\\cangqiong\\ziliao\\ziliao\\day12\\test.xlsx"));excel.write(out);//关闭资源out.close();excel.close();}

 

 读操作

@Testpublic void read() throws Exception{//输入流,读取磁盘上已经存在的excle文件InputStream in=new FileInputStream(new File("D:\\BaiduNetdiskDownload\\cangqiong\\ziliao\\ziliao\\day12\\test.xlsx"));XSSFWorkbook excle =new XSSFWorkbook(in);//读取文件中第一个sheet页XSSFSheet sheet = excle.getSheetAt(0);//获得sheet中最后一行行号int lastRowNum = sheet.getLastRowNum();for(int i=1;i<lastRowNum;i++){//获得某一行XSSFRow row=sheet.getRow(i);//获得单元格对象String cellValue1 = row.getCell(1).getStringCellValue();String cellValue2 = row.getCell(2).getStringCellValue();out.println(cellValue1+""+cellValue2);}//关闭资源in.close();excle.close();}

代码开发

实现步骤:

  1. 设计Excel模板文件
  2. 查询近30天的运营数据
  3. 将查询到的运营数据写入模板文件
  4. 通过输出流将Excel文件下载到客户端浏览器

 

 ReportController

    /*** 报表统计**/@GetMapping("/export")@ApiOperation("导出运营数据报表")public void export(HttpServletResponse response){reportService.exportBusinessData(response);}

ServiceImpl

    /*** 导出运营数据报表** @param response*/@Overridepublic void exportBusinessData(HttpServletResponse response) {//1查询数据库,获取营业数据LocalDate dateBegin = LocalDate.now().minusDays(30);//前一天LocalDate dateEnd = LocalDate.now().minusDays(1);//查询概览数据BusinessDataVO businessDataVO = workspaceService.getBusinessData(LocalDateTime.of(dateBegin, LocalTime.MIN), LocalDateTime.of(dateEnd, LocalTime.MAX));//2.通过poi将数据写入到excel中InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");//基于模板文件创建一个新的文件try {XSSFWorkbook excel = new XSSFWorkbook(in);//获取表格文件sheet标签页XSSFSheet sheet = excel.getSheet("Sheet1");//填充数据--时间sheet.getRow(1).getCell(1).setCellValue("时间:" + dateBegin + "至" + dateEnd);XSSFRow row = sheet.getRow(3);row.getCell(2).setCellValue(businessDataVO.getTurnover());row.getCell(4).setCellValue(businessDataVO.getOrderCompletionRate());row.getCell(6).setCellValue(businessDataVO.getNewUsers());//获得第五行row = sheet.getRow(4);row.getCell(2).setCellValue(businessDataVO.getValidOrderCount());row.getCell(4).setCellValue(businessDataVO.getUnitPrice());//填充明细数据for (int i=0;i<30;i++){LocalDate date=dateBegin.plusDays(i);BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));row= sheet.getRow(7+i);row.getCell(1).setCellValue(date.toString());row.getCell(2).setCellValue(businessData.getTurnover());row.getCell(3).setCellValue(businessData.getValidOrderCount());row.getCell(4).setCellValue(businessData.getOrderCompletionRate());row.getCell(5).setCellValue(businessData.getUnitPrice());row.getCell(6).setCellValue(businessData.getNewUsers());}//3通过输出流将excel文件下载到客户端浏览器ServletOutputStream out = response.getOutputStream();excel.write(out);//关闭资源out.close();excel.close();} catch (IOException e) {throw new RuntimeException(e);}}
}

完结!

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com