如何在 Java 中使用 Apache POI 获取 Excel 表格中的空行

在实际开发中,处理 Excel 文件是一个常见且重要的任务。如果你需要从 Excel 表格中提取数据,并且关心空行的处理,今天我们将通过一个简单的示例来学习如何使用 Java 的 Apache POI 库来获取 Excel 表格中的空行。

整体流程

在开始之前,让我们先了解整个流程,再通过代码逐步实现。

步骤描述
1导入 Apache POI 依赖
2读取 Excel 文件
3遍历工作表中的行
4检查每一行是否为空
5输出空行的索引

详细步骤

1. 导入 Apache POI 依赖

首先,确保在你的项目中引入 Apache POI 依赖。如果你在使用 Maven,可以在 pom.xml 中加入以下依赖:

<dependencies>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi</artifactId>
        <version>5.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>5.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.xmlbeans</groupId>
        <artifactId>xmlbeans</artifactId>
        <version>5.0.2</version>
    </dependency>
</dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
2. 读取 Excel 文件

接下来,我们需要加载一个 Excel 文件。这可以通过创建一个 FileInputStream 来实现。

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.IOException;

public class ExcelReader {
    public static void main(String[] args) {
        // 定义 Excel 文件路径
        String excelFilePath = "example.xlsx";
        try (FileInputStream fileInputStream = new FileInputStream(excelFilePath)) {
            // 创建 Workbook 实例
            Workbook workbook = new XSSFWorkbook(fileInputStream);
            // 获取第一个工作表
            Sheet sheet = workbook.getSheetAt(0);
            // 处理工作表
            processSheet(sheet);
            workbook.close(); // 关闭 Workbook
        } catch (IOException e) {
            e.printStackTrace(); // 打印异常信息
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

注释:上述代码加载了指定路径的 Excel 文件,并获取第一个工作表。

3. 遍历工作表中的行

在获得工作表后,我们需要遍历其中的每一行。

import org.apache.poi.ss.usermodel.Row;

public static void processSheet(Sheet sheet) {
    // 遍历工作表中的所有行
    for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
        Row row = sheet.getRow(rowIndex);
        if (row == null || isRowEmpty(row)) {
            System.out.println("空行 索引: " + rowIndex);
        }
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

注释:代码中使用 sheet.getLastRowNum() 获取工作表最后一行的索引,遍历每一行并判断是否为空行。

4. 检查每一行是否为空

判断一行是否为空,通常可以检查该行中的每一个单元格是否均为 null 或空字符串。

import org.apache.poi.ss.usermodel.Cell;

public static boolean isRowEmpty(Row row) {
    // 遍历每个单元格,如果有非空单元格,则返回 false
    for (int cellIndex = 0; cellIndex < row.getPhysicalNumberOfCells(); cellIndex++) {
        Cell cell = row.getCell(cellIndex);
        if (cell != null && cell.getCellType() != CellType.BLANK) {
            return false; // 只要有一个单元格非空,则该行不为空
        }
    }
    return true; // 所有单元格均为空则返回 true
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

注释:在 isRowEmpty 方法中,遍历每个单元格,若发现有非空单元格,直接返回 false

5. 输出空行的索引

在上面的代码中,空行的索引已经被打印在控制台上。你可以针对可以进一步处理或保存这些索引。

关系图

在这个过程中,可以将不同模块之间的关系形象化:

ExcelReader Workbook Sheet Row Cell read contains rows contains

结尾

通过上述步骤,你应该能够在 Java 中实现读取 Excel 表格并找到空行的功能。如果你有更多的问题,可以继续探索 Apache POI 的文档,学习更多关于单元格、数据类型、样式等的处理。努力实践,深入理解,你会发现,掌握这些工具将极大提升你的开发能力和工作效率。希望这篇文章对你入门 Java Excel 操作有帮助,祝你编程愉快!