Java POI: How to find an Excel cell with a string value and get its position (row) to use that position to find another cell

前端 未结 2 1394
陌清茗
陌清茗 2021-01-11 14:56

I\'m looking for a cell in a spreadsheet that has the string \'Total\' and then use the row in which that cell is to find the total value in another cell which is always the

2条回答
  •  甜味超标
    2021-01-11 15:37

    This method fix is the solution to your problem:

    private static int findRow(HSSFSheet 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)) {
                        return row.getRowNum();  
                    }
                }
            }
        }               
        return 0;
    }
    

    Keep in mind that your colnr is still a fixed value.

提交回复
热议问题