Apache POl

介绍

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

Apache POl 的应用场景

1.银行网银系统导出交易明细

2.各种业务系统导出Excel报表

3.批量导入业务数据

入门案例

Apache Pol的maven坐标:

XML 复制代码
             <!-- 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文件并写入文件内容

java 复制代码
  /**
     * 通过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文件并输出文件内容到控制台

java 复制代码
 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的格式过于复杂可以先创建好模版,再将数据填入

java 复制代码
/**
     * 导出运营数据报表
     * @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();
        }


    }
相关推荐
视觉震撼3 小时前
本地机器远程连接配置与文件传输可行性检测工具
运维·服务器·网络·windows·php·apache
沧海寄馀生13 小时前
Apache Hadoop生态组件部署分享-Hadoop
大数据·hadoop·分布式·apache
liu_bees1 天前
记录一次删除.jenkins目录的修复过程(完整离线部署Jenkins 2.346.1含兼容插件包)
tomcat·jenkins·apache
10km2 天前
java:Apache Commons Configuration2 占位符使用详解
java·apache·占位符·configuration2·commons·interpolator
Just_Do_IT_OK2 天前
Docker--Apache/hadoop
hadoop·docker·apache
沧海寄馀生3 天前
Apache Hadoop生态组件部署分享-zookeeper
hadoop·zookeeper·eclipse·apache·java-zookeeper
SeaTunnel3 天前
Apache SeaTunnel 如何将 CDC 数据流转换为 Append-Only 模式?
大数据·开源·apache·开发者·seatunnel·转换插件
SelectDB技术团队3 天前
公开免费!Apache Doris & SelectDB 培训与认证课程正式上线
apache
IDOlaoluo3 天前
apache-tomcat-7.0.42.tar.gz 下载与安装完整教程(附安装包)
java·tomcat·apache
DolphinScheduler社区4 天前
结项报告完整版 | 为 Apache DolphinScheduler 添加 gRPC 插件
大数据·开源·apache·海豚调度·大数据工作流调度