gpt4 book ai didi

java - 如何在 apache POI HSSF 循环中广告字符串仅匹配一次

转载 作者:行者123 更新时间:2023-12-01 21:28:26 27 4
gpt4 key购买 nike

我有一个包含类似这样的行的电子表格

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;
}
}
}

关于java - 如何在 apache POI HSSF 循环中广告字符串仅匹配一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37740445/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com