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

风格不统一 提交于 2019-12-01 16:59:57

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.

You have a semicolon after your if statement which means your if won't work:

if(cell.getRichStringCellValue().getString () == cellContent);{

Even if this won't resolve your problem, I think your while statement may not be proper for here;

while(cell.getCellType() == Cell.CELL_TYPE_STRING)

As far as I remember, there are other Cell types in POI. Try to put a breakpoint on these lines and check them if they have correct CellType.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!