如何使用 apache POI 根据 excel 中的多个列值查找数据

How to find data based on multiple column values in excel using apache POI

我正在使用 apache poi 通过 java 将值读取和写入 excel 文件。我有以下 excel 文件,我需要从该文件中获取符合我要求的标准的数据。

Application CaseID FeeType Comments
AppA 1234 Security Add Comments
AppB 1235 Other Case created
AppA 1236 Security Added Comments

在我的代码中,我想获取所有符合以下条件的案例, 应用程序设置为“AppA”,费用类型设置为“安全”,评论为“添加评论”。

我使用下面的代码使用两列作为搜索条件进行过滤,但我无法添加第三个条件。请帮助我。

ArrayList<Object> ApplicationCases = new ArrayList<Object>();

    for (Row row : sheetName) {
        for (Cell cell : row) {
            if (cell.getCellType() == CellType.STRING) {
                if (cell.getRichStringCellValue().getString().trim().equals(requiredCellContent1)) {
                    int rowNumber = row.getRowNum();

                    XSSFRow row1 = sheetName.getRow(rowNumber);
                    XSSFCell selectedCellValue = null;

                    short cellcount = row1.getLastCellNum();

                    for (int i = 0; i < cellcount; i++) {

                        if (row1.getCell(i).getRichStringCellValue().getString().trim()
                                .equals(requiredCellContent2)) {
                            selectedCellValue = row1.getCell(1);
                            ApplicationCases.add(selectedCellValue);

                        } else if (selectedCellValue == null || row1.getCell(i).getCellType() == CellType.BLANK) {
                        }
                    }
}
}

你能像下面这样吗

Iterator<Row> rows = sheet.rowIterator();
while(rows.hasNext()) {
    Row row = rows.next();
    String app = row.getCell(0).getRichStringCellValue().getString();
    String feeType = row.getCell(2).getRichStringCellValue().getString();
    String comment = row.getCell(3).getRichStringCellValue().getString();
    
    if(app.equals("AppA") && feeType.equals("Security") && comment.equals("Add Comments")) {
        //Here is your condition satisfied
    }
}