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


    }

 

 

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

拉普兰德做的到吗?

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值