当前位置: 首页 > news >正文

Apache POl

介绍

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

Apache POl 的应用场景 

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

入门案例


Apache Pol的maven坐标:

             <!-- poi --><dependency><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>3.16</version></dependency><dependency><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.16</version></dependency>

通过POI创建Excel文件并写入文件内容

  /*** 通过POI创建Excel文件并写入文件内容*/public static  void  write() throws IOException {//在内存中创建一个文件XSSFWorkbook excel = new XSSFWorkbook();//在excel文件中创建一个sheet页XSSFSheet sheet = excel.createSheet("info");//在sheet页中创建行对象,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("李四");//通过输出流将内存中的excel文件输出到磁盘FileOutputStream outputStream = new FileOutputStream(new File("B:\\info.xlsx"));excel.write(outputStream);//关闭资源outputStream.close();excel.close();}

通过POI读取已经存在的Excel文件并输出文件内容到控制台

 public  static void read() throws IOException {//读取磁盘上已经存在的Excel文件XSSFWorkbook excel = new XSSFWorkbook(new FileInputStream(new File("B:\\info.xlsx")));//读取Excel文件中的第一个Sheet页XSSFSheet sheet = excel.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();System.out.println(cellValue1 + " " +cellValue2);}}

 

如果excel的格式过于复杂可以先创建好模版,再将数据填入

/*** 导出运营数据报表* @param response*/public void exportBusinessData(HttpServletResponse response) {//1.查询数据库获取最近30天的运营数据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));//通过POI将数据写入Excel文件中InputStream in = this.getClass().getClassLoader().getResourceAsStream("template/1.xlsx");try {//基于模版文件创建一个新的Excel文件XSSFWorkbook excel = new XSSFWorkbook(in);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());}//获取一个输出流对象将excel下载的浏览器ServletOutputStream outputStream = response.getOutputStream();excel.write(outputStream);//关闭资源excel.close();outputStream.close();} catch (IOException e) {e.printStackTrace();}}

 

 

http://www.lryc.cn/news/307690.html

相关文章:

  • 高防服务器托管应注意什么
  • swagger-ui.html报错404,解决办法
  • golang 函数式编程库samber/mo使用: Future
  • 【Spring连载】使用Spring Data访问 MongoDB(十四)----Mongodb特有的查询方法
  • 消息中间件篇之RabbitMQ-消息重复消费
  • 常见设计模式之单例模式
  • VL817-Q7 USB3.0 HUB芯片 适用于扩展坞 工控机 显示器
  • 【Android安全】Windows 环境下载 AOSP 源码
  • Vue.js+SpringBoot开发快递管理系统
  • Linux/Spectra
  • C 嵌入式系统设计模式 08:硬件代理模式
  • 【k8s配置与存储--持久化存储(PV、PVC、存储类)】
  • 【Vite】解决Vite http proxy error: Error: connect ECONNREFUSED
  • FPGA领域顶级学术会议
  • 罗技鼠标滚轮模式介绍 | 鼠标滚轮异响 - 解决方案
  • Scrapy与分布式开发(2.2):正则表达式
  • 今年“全国爱耳日”主题确定!立聪堂助听器组织社区义诊
  • 区块链智能合约开发
  • Android 启动流程及 init 进程解析
  • Java设计模式:核心概述(一)
  • 计算机网络:IP
  • CSS中使用变量的两个函数var和calc
  • 了解docker与k8s
  • 服务器防火墙的应用技术有哪些
  • 打开 Camera app 出图,前几帧图像偏暗、偏色该怎样去避免?
  • SD-WAN技术:优化国内外服务器访问的关键
  • 【MySQL】学习和总结标量子查询
  • vue3第三节(v-model 执行原理)
  • RunnerGo UI自动化测试脚本如何配置
  • Android 指南针校准进度计算实现