Java excel: Get next cell in the row

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 16:24:56

问题


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

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