繁体   English   中英

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

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

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

应用 案例ID 费用类型 注释
应用程序A 1234 安全 添加评论
应用程序B 1235 其他 已创建案例
应用程序A 1236 安全 添加评论

在我的代码中,我想获取所有符合以下条件的案例,应用程序设置为“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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM