如何在 apache POI HSSF 中循环广告字符串匹配一次
How to ads string match only once in loop in apache POI HSSF
我有一个包含类似这样的行的电子表格
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 出现在该行的任何位置,它就会匹配。
一旦在任何单元格中找到匹配项,就跳出单元格迭代:
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;
}
}
}
我有一个包含类似这样的行的电子表格
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 出现在该行的任何位置,它就会匹配。
一旦在任何单元格中找到匹配项,就跳出单元格迭代:
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;
}
}
}