我有一个电子表格,我只想选择一列中有特定单词而另一列中有另一个单词的行。目前我只能检查行中任何单元格中的单词。如何检查特定列中单元格中是否存在该单词?
这是我的代码
FileInputStream fis = new FileInputStream(new File(filepath));
XSSFWorkbook workBook = new XSSFWorkbook (fis);
XSSFSheet sheet = workBook.getSheetAt (0);
List<XSSFRow> filteredRows = new ArrayList<XSSFRow>();
Iterator<Row> rows= sheet.rowIterator();
while (rows.hasNext ()){
XSSFRow row = (XSSFRow) rows.next ();
Iterator<Cell> cells = row.cellIterator ();
while (cells.hasNext ()){
XSSFCell cell = (XSSFCell) cells.next ();
if (cell.toString().contains("Ginko")) {
filteredRows.add(row);
break;
}
}
}
答案 0 :(得分:2)
您可以通过调用cell.getColumnIndex() == columnNumber
来检查某个单元格是否在特定列中。例如:
for(Cell cell : row) {
if(cell.getColumnIndex() == 1) {
// process a cell in column 1
}
}
来源:https://poi.apache.org/apidocs/org/apache/poi/ss/usermodel/Cell.html#getColumnIndex()