错误:如何在Excel中读取空单元格

时间:2012-06-22 11:35:39

标签: java excel apache-poi

我正在尝试使用POI从excel读取数据。如何检查这是否为空单元格?

我不知道遗失了什么我认为这应该有效:

java.util.Iterator<Row> rows = worksheet.rowIterator();

HSSFRow row = (HSSFRow) rows.next();
HSSFCell cellF1 = (HSSFCell) row.getCell(5);
if(cellF1.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
  String val = "";
}

我在if语句(空指针)中遇到错误,但只有在我使用它时我才能检查:

   while (rows.hasNext()) {
    HSSFRow row = (HSSFRow) rows.next();
    java.util.Iterator<Cell> cells = row.cellIterator();
    while (cells.hasNext()) {
      HSSFCell cell = (HSSFCell) cells.next();
      if(cell.getCellType() == HSSFCell.CELL_TYPE_BLANK) {
        String emptytype = "";
        System.out.println("empty");
      }
    }
  }

5 个答案:

答案 0 :(得分:9)

这是Row.getCell的单参数版本的正常行为。如果查看API文档,它会明确指出如果未定义单元格,getCell将返回null。许多java函数都表现出这种行为,因此编写代码时没有任何问题。因此,您的代码的一个版本可能是:

boolean hasDataFlag = true;    
HSSFRow row = sheet.getRow(rowNumber);
hasDataFlag = (row != null);
HSSFCell cell = null;
if (hasDataFlag) cell = row.getCell(cellNumber);
hasDataFlag = (cell != null);
if (hasDataFlag) hasDataFlag = (cell.getCellType() != Cell.CELL_TYPE_BLANK);
if (hasDataFlag) {
    // process the cell here
}

或者,您可以使用Row.getCell的其他版本,它使用第二个参数指定缺少的单元格策略。此版本允许您指定getCell为空单元格返回空单元格。所以,这里有一些althernative代码:

HSSFRow row = sheet.getRow(rowNumber);
if (row != null) {
    HSSFCell cell = row.getCell(cellNumber, Row.RETURN_BLANK_AS_NULL);
    if (cell != null) {
        // process cell here
    }
}

或者,如果您愿意,可以将策略指定为Row.CREATE_NULL_AS_BLANK。在这种情况下,您可以将if (cell != null)替换为if (cell.getCellType() != Cell.CELL_TYPE_BLANK)

答案 1 :(得分:5)

如果您使用CellIterator,您将只获得已在某个时间点定义的单元格(无null个单元格,但您将获得blank个单元格。如果要获取所有单元格,请按索引

获取它们

通过索引,您可以执行以下操作:

 Sheet sheet = workbook.getSheetAt(0);
 for (int rowNumber = sheet.getFirstRowNum(); rowNumber <= sheet.getLastRowNum(); rowNumber++) {
    Row row = sheet.getRow(rowNumber);
    if (row == null) {
         // This row is completely empty
    } else {
         // The row has data
         for (int cellNumber = row.getFirstCellNum(); cellNumber <= row.getLastCellNum(); cellNumber++) {
             Cell cell = row.getCell(cellNumber);
             if (cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK) {
                 // This cell is empty
             } else {
                 // This cell has data in it
             }
         }
    }
 }

答案 2 :(得分:1)

Cell cell = row.getCell(x, Row.CREATE_NULL_AS_BLANK);

这个诡计让我很开心。 看看它对你有用......

答案 3 :(得分:0)

int column, rownumber = 0; // just for showing the example code

Row row = sheet1.getRow(rownumber); // just for showing the example code

if (row!= null)
    mycell = row.getCell(0);
if (mycell != null && mycell.getCellType() != Cell.CELL_TYPE_BLANK) {
    System.out.println(mycell.getStringCellValue());
}
else{
    System.out.println("whatever");  
}

答案 4 :(得分:0)

public String getCellData(String File_Name,int Sheet_Num,int Row_Num,int Col_Num)抛出IOException {

    FileInputStream fileIn = new FileInputStream(System.getProperty("user.dir")+"\\"+File_Name+".xlsx");
    XSSFWorkbook workbook = new XSSFWorkbook(fileIn);
    XSSFSheet sheet=workbook.getSheetAt(Sheet_Num-1);
    XSSFRow row = sheet.getRow(Row_Num-1);
    XSSFCell cell= row.getCell(Col_Num-1);
    String celldata;
    if(cell!=null){
    celldata= cell.getStringCellValue();
    }
    else 
        celldata ="";
    fileIn.close();
    return celldata;

    }