[英]How to find data based on multiple column values in excel using apache POI
I am using the apache poi for reading and writing values to excel file with java.我正在使用 apache poi 使用 java 读取和写入 excel 文件的值。 I have below excel file, from that file i need to fetch data which comes under my required criteria.
我有以下 excel 文件,我需要从该文件中获取符合我要求的标准的数据。
Application![]() |
CaseID![]() |
FeeType![]() |
Comments![]() |
---|---|---|---|
AppA![]() |
1234 ![]() |
Security![]() |
Add Comments![]() |
AppB![]() |
1235 ![]() |
Other![]() |
Case created![]() |
AppA![]() |
1236 ![]() |
Security![]() |
Added Comments![]() |
In my code I want to get all the cases which comes under below criteria, Application set to "AppA",Fee Type set to "Security" and Comments with "Add Comments".在我的代码中,我想获取所有符合以下条件的案例,应用程序设置为“AppA”,费用类型设置为“安全”,评论带有“添加评论”。
I used below code to filter using two columns for search criteria, but i cannot add the third criteria.我使用下面的代码使用两列作为搜索条件进行过滤,但我无法添加第三个条件。 Please help me.
请帮我。
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) {
}
}
}
}
Can you so something like below你能像下面这样吗
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.