Error: How to read empty cell in Excel

╄→尐↘猪︶ㄣ 提交于 2019-12-12 15:55:22

问题


I'm trying to read data from excel using POI. How can I check if that is an empty cell?

I don't know what is missing I think this should be working:

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

I got error in if statement (null pointer), but only if I use this I can check that:

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

回答1:


This is normal behavior for the 1-argument version of Row.getCell. If you look at the API doc, it specifically states that getCell will return null if the cell is not defined. Many java functions exhibit this sort of behavior, so there is nothing wrong with coding to take this into account. So, one version of your code could be something like:

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
}

Alternatively, you could use the other version of Row.getCell, which takes a second argument that specifies the missing cell policy. This version would allow you to specify that getCell return a null cell for blank cells. So, here is some althernative code:

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

Or, if you prefer, you could specify the policy as Row.CREATE_NULL_AS_BLANK. In that case, you would replace if (cell != null) with if (cell.getCellType() != Cell.CELL_TYPE_BLANK).




回答2:


If you use the CellIterator, you'll only get the cells that have been defined at some point (no null cells, but you will get blank cells). If you want to get all cells, fetch them by index

By index, you'd do something like:

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



回答3:


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

THis trick heped me a lot. See if it's useful for you...




回答4:


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



回答5:


public String getCellData(String File_Name,int Sheet_Num, int Row_Num, int Col_Num) throws 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;

    }


来源:https://stackoverflow.com/questions/11155428/error-how-to-read-empty-cell-in-excel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!