gpt4 book ai didi

java - 使用 POI 从 Excel 工作表的下一行获取值

转载 作者:行者123 更新时间:2023-12-01 18:49:29 25 4
gpt4 key购买 nike

我正在使用 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();
}

...

最佳答案

您可以使用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();

关于java - 使用 POI 从 Excel 工作表的下一行获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29539428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com