错误:如何读取Excel中的空单元格 [英] Error: How to read empty cell in Excel

查看:915
本文介绍了错误:如何读取Excel中的空单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图读取使用POI从Excel中的数据。如何检查,如果这是一个空单元格?

我不知道缺什么我想这应该是工作:

  java.util.Iterator的<行>行= worksheet.rowIterator();HSSFRow行=(HSSFRow)rows.next();
HSSFCell cellF1 =(HSSFCell)row.getCell(5);
如果(cellF1.getCellType()== HSSFCell.CELL_TYPE_BLANK){
  串VAL =;
}

我if语句(空指针)有错误,但只有当我使用这个我可以检查:

 而(rows.hasNext()){
    HSSFRow行=(HSSFRow)rows.next();
    java.util.Iterator的<电池>细胞= row.cellIterator();
    而(cells.hasNext()){
      HSSFCell细胞=(HSSFCell)cells.next();
      如果(cell.getCellType()== HSSFCell.CELL_TYPE_BLANK){
        串emptytype =;
        的System.out.println(空);
      }
    }
  }


解决方案

这是Row.getCell的1参数版本的正常行为。如果你看一下API文档,它明确规定,如果没有定义单元格getCell将返回null。许多Java功能表现出这种行为,所以没有什么错编码,以考虑到这一点。所以,你的code的一个版本可能是这样的:

 布尔hasDataFlag = TRUE;
HSSFRow行= sheet.getRow(ROWNUMBER);
hasDataFlag =(行!= NULL);
HSSFCell细胞=无效;
如果(hasDataFlag)细胞= row.getCell(cellNumber);
hasDataFlag =(细胞!= NULL);
如果(hasDataFlag)hasDataFlag =(cell.getCellType()= Cell.CELL_TYPE_BLANK!);
如果(hasDataFlag){
    //过程中的细胞在这里
}

另外,你可以使用其他版本的Row.getCell,这需要指定缺少的格策第二个参数。这个版本将允许您指定getCell返回空单元格为空白单元格。所以,这里是一些althernative code:

  HSSFRow行= sheet.getRow(ROWNUMBER);
如果(行!= NULL){
    HSSFCell细胞= row.getCell(cellNumber,Row.RETURN_BLANK_AS_NULL);
    如果(细胞!= NULL){
        //此处过程单元
    }
}

或者,如果你preFER,您可以指定政策Row.CREATE_NULL_AS_BLANK。在这种情况下,你将取代如果(细胞!= NULL)改为如果(cell.getCellType()!= Cell.CELL_TYPE_BLANK)。

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

解决方案

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)".

这篇关于错误:如何读取Excel中的空单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆