我有一个包含很多行的电子表格
SomeText1 SomeText2 SomeText3
Tommy Tommy John
Gilaen Tamara Shaz
Tamara Phil Tamara
我想过滤包含' Tamara'所以我应该得到的行将是
SomeText1 SomeText2 SomeText3
Gilaen Tamara Shaz
Tamara Phil Tamara
要尝试这样做,我使用以下代码
Map<String,String> mapEndoscBarr= new LinkedHashMap<String,String>();
FileInputStream fis = new FileInputStream(new File(filepath));
HSSFWorkbook workBook = new HSSFWorkbook (fis);
HSSFSheet sheet = workBook.getSheetAt (0);
List<HSSFRow> filteredRows = new ArrayList<HSSFRow>();
//Filter by pathology from what I am given
Iterator<Row> rows= sheet.rowIterator();
while (rows.hasNext ()){
HSSFRow row = (HSSFRow) rows.next ();
Iterator<Cell> cells = row.cellIterator ();
while (cells.hasNext ()){
HSSFCell cell = (HSSFCell) cells.next ();
if (cell.toString().contains("Tamara")) {
filteredRows.add(row);
}
}
}
然而,每次找到匹配时循环都会添加行,这样我最终会得到重复的行,如下所示:
SomeText1 SomeText2 SomeText3
Gilaen Tamara Shaz
Tamara Phil Tamara
Tamara Phil Tamara
我如何才能强行进行一场比赛,因此如果Tamara出现在该行的任何位置,我就不会得到这个副本。
答案 0 :(得分:3)
在任何单元格中找到匹配项后,只需退出单元格迭代:
Map<String,String> mapEndoscBarr= new LinkedHashMap<String,String>();
FileInputStream fis = new FileInputStream(new File(filepath));
HSSFWorkbook workBook = new HSSFWorkbook (fis);
HSSFSheet sheet = workBook.getSheetAt (0);
List<HSSFRow> filteredRows = new ArrayList<HSSFRow>();
//Filter by pathology from what I am given
Iterator<Row> rows= sheet.rowIterator();
while (rows.hasNext ()){
HSSFRow row = (HSSFRow) rows.next ();
Iterator<Cell> cells = row.cellIterator ();
while (cells.hasNext ()){
HSSFCell cell = (HSSFCell) cells.next ();
if (cell.toString().contains("Tamara")) {
filteredRows.add(row);
break;
}
}
}