使用 POI 从 Excel sheet 的下一行获取值

Get value from next row in Excel sheet with POI

我正在使用 Apache POI 阅读 Excel 文档。如果当前为空,我想从下一行获取单元格值。例如,从第一行获取所有非空单元格值,对于空单元格:移动到下一行并获取值。 这是我用来阅读 Excel 文档

的代码
                //every sheet has rows, iterate over them
                Iterator<Row> rowIterator = sheet.iterator();

                if (rowIterator.hasNext())
                    rowIterator.next();

                while (rowIterator.hasNext()) 
                {

                    String libelle="";
                    .....

                    //Get the row object
                    Row row = rowIterator.next();

                    //Every row has columns, get the column iterator and iterate over them
                    Iterator<Cell> cellIterator = row.cellIterator();

                    while (cellIterator.hasNext()) 
                    {
                        //Get the Cell object
                        Cell cell = cellIterator.next();

                        //check the cell type and process accordingly
                        //2nd column

                        switch(cell.getCellType()){
                        case Cell.CELL_TYPE_STRING:

                            ......
                            if (row.getCell(5) == null)
                            {
                               System.out.println("Cell is Empty in Column:" );

                            } else if (row.getCell(5).getCellType() == HSSFCell.CELL_TYPE_STRING)
                            {
                            libelle= row.getCell(5).getStringCellValue().trim();
                            }

...

您可以找出以下RowCell的当前Cell.getRowIndex() to access the next Row via the Sheet.getRow(int) method if the calculated index is smaller than Sheet.getLastRowNum() in order to access Cell.getColumnIndex()

您可以使用 Sheet.getRow(int rownumber) 获取具有特定数字的行。 您可以通过 Row.getRowNum() 获取当前行的编号,通过 Cell.getColumnIndex().

获取单元格的索引

因此,下一行单元格的值为

Row nextRow = sheet.getRow(row.getRowNum() + 1);
if (nextRow != null)
    String value = nextRow.getCell(curCel.getColumnIndex(), Row.CREATE_NULL_AS_BLANK).getStringCellValue();