问题
I am trying to get the value of the next cell in a after a string is found. Let's say I get A1 as the cell that contains my search string "here", I am trying to get the contents of the next cell after A1 in the row.
private static String findRow(XSSFSheet sheet, String cellContent) {
for (Row row : sheet) {
for (Cell cell : row) {
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim().equals(cellContent)) {
cell = cell.getNextRowCell //made up method
return cell.getStringCellValue();
}
}
}
回答1:
I figured it out.
private static String getNextRow(XSSFSheet sheet, String cellContent) {
Iterator<Row> itr = sheet.iterator();
while (itr.hasNext()) {
Row row = itr.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
if (cell.getRichStringCellValue().getString().trim()
.equals(cellContent)) {
row = itr.next();
int val = cell.getColumnIndex();
cell = row.getCell(val);
String srow = cell.getStringCellValue();
return srow;
}
}
}
}
return null;
}
来源:https://stackoverflow.com/questions/26370337/java-excel-get-next-cell-in-the-row