简体   繁体   English

如何在读取Excel工作表时知道单元格是否返回空值?

[英]How do I know if a cell returns a null value while reading an Excel Sheet?

I am reading values from some specific cells in an excel sheet. 我正在读取excel表中某些特定单元格的值。 Those cells generally contain String but sometimes they may be blank (thus may return a blank or a null). 这些单元格通常包含String,但有时它们可​​能是空白的(因此可能返回空格或null)。 I am using the following code to get the cell data: 我使用以下代码来获取单元格数据:

Cell cellValue = row.getCell(10, Row.RETURN_NULL_AND_BLANK);
List.add(cellValue.getStringCellValue());

However, when I run this code I get a NullPointerException . 但是,当我运行此代码时,我得到一个NullPointerException Can anybody point out what is wrong and should be added? 任何人都可以指出什么是错的,应该加入吗?

Note: I am deliberately using Row.RETURN_NULL_AND_BLANK and not MissingCellPolicy.RETURN_NULL_AND_BLANK because using the latter was giving me an error. 注意:我故意使用Row.RETURN_NULL_AND_BLANK而不是MissingCellPolicy.RETURN_NULL_AND_BLANK因为使用后者会给我一个错误。

You need to test the cellValue if it is not null, since using Row.RETURN_NULL_AND_BLANK will return null in cases where you are trying to access a missing cell. 如果cellValue不为null,则需要测试它,因为在尝试访问缺少的单元格时,使用Row.RETURN_NULL_AND_BLANK将返回null

You are getting a NullPointerException because the 10-th column on a specified row is missing. 您将收到NullPointerException因为缺少指定行上的第10列。

If you need to handle differently the missing vs. blank cases you can use the following: 如果您需要处理丢失与空白案例的不同,您可以使用以下内容:

Cell cellValue = row.getCell(10, Row.RETURN_NULL_AND_BLANK);
if (cellValue == null)
    List.add("CELL NOT FOUND");
else if("".equals(cellValue.getStringCellValue().trim()))
    List.add("MISSING CONTENT");
else 
    List.add(cellValue.getStringCellValue());

@dan - Thanks for your reply. @dan - 谢谢你的回复。 That indeed helped. 这确实有帮助。 I modified my code as follows and I am not getting that exception now: 我按如下方式修改了代码,现在我没有得到异常:

Cell cellValue = row.getCell(10, Row.CREATE_NULL_AS_BLANK);
if(cellValue.getStringCellValue().equals("")){
    List.add("NOT FOUND");
}
else {
    List.add(cellValue.getStringCellValue());
}

hey there , why cant you add the null pointer check like 嘿那里,为什么你不能添加空指针检查

if(null!=cellValue)

    {

    List.add(cellValue.getStringCellValue());
}

hope this helps if the cell value is null then you dont add the cell value to your list. 希望这有助于如果单元格值为null,那么您不会将单元格值添加到列表中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取Excel文件时Cell.getNumericCellValue返回空值 - Cell.getNumericCellValue returns null value when reading Excel file 读取Excel文件时如何识别单元格为空,为空或为空 - How to identify a cell is blank or null or empty while reading a excel file 从Excel工作表读取时值不正确 - Incorrect value while reading from excel sheet 在Java中读取Excel工作表时,它倾向于跳过一个单元格的值,并用下一个列自动填充它 - while reading excel sheet in java it tends to skip one cell value and auto populate it with next colomn 如何使用Java处理Excel工作表中的空值 - How can I deal with null value in excel sheet using Java 读取Excel工作表的空单元格 - Reading Empty Cell of Excel Sheet 如果不返回任何空值,如何编写将值传递给Excel工作表中单元格的条件? - How to write the condition for passing a value to a cell in excel sheet if is not returning any null value? 我如何通过Java检查Excel文件中我的单元格对于特定的第一个单元格值是否不为空 - how do i check that my cells in a an excel file are not null for a specific 1st cell value, through java 如果第一个和最后一个单元格值为空或空白,如何读取 Excel 工作表行的每个单元格的值? - How to read each and every cell's value of the row of Excel sheet if 1st and last cell value is Null or Blank? 从 java 中的 excel 读取单元格数据时,它返回十进制值 - When reading cell data from excel in java it returns decimal value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM