cellIterator.hasNext() returns false 即使单元格有值

cellIterator.hasNext() returns false even the cell has value

我使用以下方法从 excel (csv) sheet:

中读取数据
InputStream inputStream = new BufferedInputStream(multipartFile.getInputStream());
XSSFWorkbook myWorkBook = new XSSFWorkbook(inputStream);
List<String> cellList = new ArrayList<>();
XSSFSheet mySheet = myWorkBook.getSheetAt(0);

Iterator<Row> rowIterator = mySheet.iterator();
rowIterator.next();

while (rowIterator.hasNext()) {
    Row row = rowIterator.next();
    cellList.clear();
    Iterator<Cell> cellIterator = row.cellIterator();

    while (cellIterator.hasNext()) { // this returns false even the cell has data 
        Cell cell = cellIterator.next();
        cellList.add(cell.getStringCellValue());
    }
}

我想让 cellIterator.hasNext() returns 对于第二行(名称)中的空单元格值为真。那么,我该怎么做呢?

Create Date  Uuid                                  Name
2021-09-29   2e81a2b6-3226-4fe0-b8bd-39f42239686d  admin
2021-09-30   610e9040-840c-48c5-aa64-f193ed896133  

对于空的“名称”单元格,您不能使 cellIterator.hasNext() return 为真。

如果事先知道列数,可以使用循环和row.getCell(index)

        for (int i = 0; i < 3; i++) {
            Cell cell = row.getCell(i);
            cellList.add(cell == null ? "Empty":cell.toString());
        }

如果不这样做,则必须首先扫描文件以计算列数。