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();
        }


    }
相关推荐
一次旅行1 天前
【数据分析/可视化】Apache Superset企业级BI数据可视化平台实战详解
信息可视化·数据分析·apache
万岳科技系统开发2 天前
互联网医院小程序搭建如何快速上线?完整建设方案解析
小程序·apache
南山丶无梅落3 天前
文件上传漏洞1
apache·绕过·文件上传漏洞·网安·upload闯关·文件类型验证
kke_884 天前
电商/教育/工具类小程序,UV分析的3种不同思路
大数据·apache
nvd114 天前
深度解析:Apache Beam YAML 部署至 GCP Dataflow 的架构与最佳实践
架构·apache
27669582925 天前
拼多多m端/小程序 encrypt_info
java·小程序·apache·encrypt_info·encrypt_info解密·拼多多小程序·拼多多m端
ylscode6 天前
Apache CXF LDAP注入漏洞允许攻击者获取任意证书
apache
MageGojo9 天前
小程序每日一谜怎么做:riddle 接口接入示例
windows·小程序·apache·谜语
penngo9 天前
FlowLoom:基于 Apache Spark 的可视化数据处理平台
大数据·spark·apache
Apache RocketMQ10 天前
全新 AI 消息模型:Apache RocketMQ 如何让 AI 应用拥抱事件驱动架构?
人工智能·apache·rocketmq