Apache POI的使用

系列文章目录



前言

Apache POI:提供 API 给 Java 程序对 Microsoft Office 格式文件(.xlsx文件)进行读写操作。

一、使用

1.导入依赖

# poi用于处理 Excel 97-2003 格式(.xls)的文件,
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
# poi-ooxml用于处理 Excel 2007 及以上格式(.xlsx)的文件。
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>

2.方法

以下是一些 Apache POI 中常用的方法及解释:

  1. Workbook workbook = new XSSFWorkbook(new FileInputStream(new File("yourExcel.xlsx")));

    • 用于创建一个工作簿对象,并从指定的 Excel 文件(.xlsx 格式)读取数据。
  2. Sheet sheet = workbook.getSheetAt(0);

    • 通过索引获取工作簿中的工作表,索引从 0 开始。
  3. Row row = sheet.getRow(0);

    • 获取指定行,索引从 0 开始。
  4. Cell cell = row.getCell(0);

    • 获取指定单元格,索引从 0 开始。
  5. cell.setCellValue("New Value");

    • 设置单元格的值。
  6. int rowCount = sheet.getPhysicalNumberOfRows();

    • 获取工作表中实际存在的行数。
  7. int columnCount = row.getPhysicalNumberOfCells();

    • 获取指定行中实际存在的单元格数量。
  8. DataFormatter formatter = new DataFormatter();

    • 创建一个数据格式化器,用于将单元格中的数据以合适的格式获取。
  9. String cellValue = formatter.formatCellValue(cell);

    • 按照合适的格式获取单元格的值。
  10. workbook.write(new FileOutputStream(new File("newExcel.xlsx")));

    • 将修改后的工作簿数据写入到新的 Excel 文件中。

二、使用POI要用到的一些其它方法

用于通过类加载器获取资源的输入流。
通常用于读取项目中的资源文件

InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");

示例

public void exportBusinessData(HttpServletResponse response) {
    // 获取当前日期的前 30 天作为起始日期
    LocalDate begin = LocalDate.now().minusDays(30);
    // 获取当前日期的前 1 天作为结束日期
    LocalDate end = LocalDate.now().minusDays(1);

    // 查询指定时间段的概览运营数据,并提供给 Excel 模板文件
    BusinessDataVO businessData = workspaceService.getBusinessData(LocalDateTime.of(begin, LocalTime.MIN), LocalDateTime.of(end, LocalTime.MAX));

    // 从类加载器中获取 Excel 模板文件的输入流
    InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("template/运营数据报表模板.xlsx");
    try {
        // 基于提供的模板文件输入流创建一个新的 Excel 工作簿对象
        XSSFWorkbook excel = new XSSFWorkbook(inputStream);
        // 获取 Excel 文件中的"Sheet1"工作表
        XSSFSheet sheet = excel.getSheet("Sheet1");

        // 设置工作表第 1 行第 1 个单元格的值为起始日期至结束日期
        sheet.getRow(1).getCell(1).setCellValue(begin + "至" + end);
        // 获取工作表的第 4 行
        XSSFRow row = sheet.getRow(3);
        // 设置第 4 行第 2 个单元格的值为获取的业务数据中的营业额
        row.getCell(2).setCellValue(businessData.getTurnover());
        // 设置第 4 行第 4 个单元格的值为获取的业务数据中的订单完成率
        row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
        // 设置第 4 行第 6 个单元格的值为获取的业务数据中的新用户数量
        row.getCell(6).setCellValue(businessData.getNewUsers());

        // 再次获取工作表的第 4 行
        row = sheet.getRow(4);
        // 设置第 4 行第 2 个单元格的值为获取的业务数据中的有效订单数量
        row.getCell(2).setCellValue(businessData.getValidOrderCount());
        // 设置第 4 行第 4 个单元格的值为获取的业务数据中的单价
        row.getCell(4).setCellValue(businessData.getUnitPrice());

        // 循环 30 次,处理每天的明细数据
        for (int i = 0; i < 30; i++) {
            // 起始日期加上循环次数得到当天日期
            LocalDate date = begin.plusDays(i);
            // 准备当天的明细数据
            businessData = workspaceService.getBusinessData(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
            // 获取工作表的第 7 + i 行
            row = sheet.getRow(7 + i);
            // 设置第 7 + i 行第 1 个单元格的值为当天日期的字符串形式
            row.getCell(1).setCellValue(date.toString());
            // 设置第 7 + i 行第 2 个单元格的值为当天的营业额
            row.getCell(2).setCellValue(businessData.getTurnover());
            // 设置第 7 + i 行第 3 个单元格的值为当天的有效订单数量
            row.getCell(3).setCellValue(businessData.getValidOrderCount());
            // 设置第 7 + i 行第 4 个单元格的值为当天的订单完成率
            row.getCell(4).setCellValue(businessData.getOrderCompletionRate());
            // 设置第 7 + i 行第 5 个单元格的值为当天的单价
            row.getCell(5).setCellValue(businessData.getUnitPrice());
            // 设置第 7 + i 行第 6 个单元格的值为当天的新用户数量
            row.getCell(6).setCellValue(businessData.getNewUsers());
        }
        // 通过响应的输出流将生成的 Excel 文件下载到客户端浏览器
        ServletOutputStream out = response.getOutputStream();
        excel.write(out);
        // 刷新输出流,确保数据完全输出
        out.flush();
        // 关闭输出流
        out.close();
        // 关闭 Excel 工作簿
        excel.close();

    } catch (IOException e) {
        // 打印输入输出异常的堆栈跟踪信息
        e.printStackTrace();
    }
}
  • 8
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值