Java excel:获取行中的下一个单元格

时间:2014-10-14 21:07:19

标签: java excel apache-poi

我试图在找到字符串之后获取下一个单元格的值。让我们说我将A1作为包含我的搜索字符串的单元格"在这里",我试图在行中的A1之后获取下一个单元格的内容。

 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 个答案:

答案 0 :(得分:0)

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;
    }