Java 如何使用ApachePOI检查excel单元格是否为空?
我正在使用Poi.jar从excel表格中获取输入,想知道如何检查单元格是否为空 现在我正在使用下面的代码Java 如何使用ApachePOI检查excel单元格是否为空?,java,apache-poi,Java,Apache Poi,我正在使用Poi.jar从excel表格中获取输入,想知道如何检查单元格是否为空 现在我正在使用下面的代码 cell = myRow.getCell(3); if (cell != null) { cell.setCellType(Cell.CELL_TYPE_STRING); //System.out.print(cell.getStringCellValue() + "\t\t"); if (cell.getStringCellValue() != "")
cell = myRow.getCell(3);
if (cell != null) {
cell.setCellType(Cell.CELL_TYPE_STRING);
//System.out.print(cell.getStringCellValue() + "\t\t");
if (cell.getStringCellValue() != "")
depend[p] = Integer.parseInt(cell.getStringCellValue());
}
}
如果您使用的是ApachePOI 4.x,则可以通过以下方式实现:
Cell c = row.getCell(3);
if (c == null || c.getCellType() == CellType.Blank) {
// This cell is empty
}
对于较早的Apache POI 3.x版本,在迁移到CellType
enum之前,它是:
Cell c = row.getCell(3);
if (c == null || c.getCellType() == Cell.CELL_TYPE_BLANK) {
// This cell is empty
}
但是不要忘记检查
行
是否为空-如果该行从未在没有使用过单元格或没有设置过单元格样式的情况下使用过,则该行本身可能为空 还有一个选择
row=(Row) sheet.getRow(i);
if (row == null || isEmptyRow(row)) {
return;
}
Iterator<Cell> cells = row.cellIterator();
while (cells.hasNext())
{}
row=(row)sheet.getRow(i);
如果(行==null | | isEmptyRow(行)){
返回;
}
迭代器单元格=行。单元格迭代器();
while(cells.hasNext())
{}
Gagravarr的答案很好
检查excel单元格是否为空 但是如果假设单元格包含空字符串(
“
”),则需要一些额外的代码。如果单元格被编辑后未正确清除(有关如何正确清除单元格,请参阅下文),则可能会发生这种情况
我给自己写了一个助手来检查XSSFCell
是否为空(包括一个空字符串)
注意更新的POI版本
他们首先将getCellType()
版本更改为getCellTypeEnum()
版本3.15 Beta 3
,然后将getCellType()
版本更改为4.0
- 版本
:=3.15 Beta 3
- 使用
和CellType.BLANK
代替CellType.STRING
和Cell.Cell\u TYPE\u BLANK
Cell.Cell\u TYPE\u STRING
- 使用
- 版本
&版本=3.15 Beta 3
<4.0
- 使用
而不是Cell.getCellTypeEnum()
Cell.getCellType()
- 使用
例子 这个JUnit测试显示了需要额外的空检查的情况 场景:在Java程序中更改单元格的内容。稍后,在同一个Java程序中,检查单元格是否为空。如果
isCellEmpty(XSSFCell单元格)
函数未检查空字符串,测试将失败
@Test
public void testIsCellEmpty_CellHasEmptyString_ReturnTrue() {
// Arrange
XSSFCell cell = new XSSFWorkbook().createSheet().createRow(0).createCell(0);
boolean expectedValue = true;
boolean actualValue;
// Act
cell.setCellValue("foo");
cell.setCellValue("bar");
cell.setCellValue(" ");
actualValue = isCellEmpty(cell);
// Assert
Assert.assertEquals(expectedValue, actualValue);
}
另外:正确地清除单元格 以防有人想知道如何正确清除单元格内容。有两种方法可以存档(我推荐方法1) 为什么是第一条路显式优于隐式(谢谢,Python) 方式1:将单元格类型显式设置回空白 方式2:由于将单元格值设置为
null
字符串时会产生副作用,因此将单元格类型隐式设置回blank
有用的来源
.getCellType() != Cell.CELL_TYPE_BLANK
这个技巧对我帮助很大,看看它是否对您有用。从Apache POI 3.17开始,您必须使用枚举检查单元格是否为空:
import org.apache.poi.ss.usermodel.CellType;
if(cell == null || cell.getCellTypeEnum() == CellType.BLANK) { ... }
首先,为了避免NullPointerException,您必须添加以下内容
Row.MissingCellPolicy.CREATE_NULL_AS_BLANK
这将创建一个空白单元格,而不是给您NPE,然后您可以检查以确保没有出现任何问题,就像@Gagravarr所说的那样
Cell cell = row.getCell(j, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);
if (cell == null || cell.getCellTypeEnum() == CellType.BLANK)
// do what you want
Cell.getCellType()
在最新的POI API中被弃用。如果您使用的是POI API版本3.17,请使用以下代码:
if (Cell.getCellTypeEnum() == CellType.BLANK) {
//do your stuff here
}
String empty = "-";
if (row.getCell(3) == null || row.getCell(3).getCellType() == Cell.CELL_TYPE_BLANK) {
upld.setValue(empty);
} else {
upld.setValue(row.getCell(3).getStringCellValue());
}
这是我从POI 3.1.7到POI 4看到的最安全、最简洁的方式:
boolean isBlankCell = CellType.BLANK == cell.getCellTypeEnum();
boolean isEmptyStringCell = CellType.STRING == cell.getCellTypeEnum() && cell.getStringCellValue().trim().isEmpty();
if (isBlankCell || isEmptyStringCell) {
...
从POI 4开始,如果支持
getCellType()
,则getCellTypeEnum()
将被弃用,但返回类型应保持不变。您也可以使用开关大小写,如
String columndata2 = "";
if (cell.getColumnIndex() == 1) {// To match column index
switch (cell.getCellType()) {
case Cell.CELL_TYPE_BLANK:
columndata2 = "";
break;
case Cell.CELL_TYPE_NUMERIC:
columndata2 = "" + cell.getNumericCellValue();
break;
case Cell.CELL_TYPE_STRING:
columndata2 = cell.getStringCellValue();
break;
}
}
System.out.println("Cell Value "+ columndata2);
请尝试以下代码:
if (Cell.getCellTypeEnum() == CellType.BLANK) {
//do your stuff here
}
String empty = "-";
if (row.getCell(3) == null || row.getCell(3).getCellType() == Cell.CELL_TYPE_BLANK) {
upld.setValue(empty);
} else {
upld.setValue(row.getCell(3).getStringCellValue());
}
Row.MissingCellPolicy.CREATE\u NULL\u AS\u BLANK在我的例子中是可行的
total_colume = myRow.getLastCellNum();
int current_colume = 0;
HSSFCell ReadInCellValue;
while (current_colume <= total_colume) {
ReadInCellValue = myRow.getCell(current_colume, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);//if cell is empty, return black
if (ReadInCellValue.toString=="") Log.d("empty cell", "colume=" + String.valuOf(current_colume));
current_colume++;
}
total_colume=myRow.getLastCellNum();
int当前列=0;
HSSFCell ReadInCellValue;
while(当前列if(cell.getStringCellValue()!=“”)是错误的。
如果(!cell.getStringCellValue().equals(“”))应该是
。在某些情况下,这会给我带来NPE。您需要确保行不为空-这里的代码对于非空行可以正常工作,因为它已经包含单元格空检查!嗯,如果(row==null){//对空行执行操作},那么呢
?@Winklerr Excel应将空字符串作为空白单元格写入,因此不应写入happen@irJvV您只需等待Apache POI 4.0,它就不会出现,它是从int到enum的洗牌过程的一部分!检查当前迭代的单元格是否为空的位置在哪里?它与数字的关系如何?单元格的类型是否为NUMERIC且仍然为空?@JanZyk如果是“否”,则数字单元格的值至少应为0
。因此您可以在工作表中看到该值。数字单元格不能为“空”这样做是因为您不能将单元格的值设置为null
double值。正如您所看到的,double值的setter只接受原语double类型。与此相反,string值的setter也接受null
值,这将导致单元格类型自动转换为CellType.BLANK
@Sandro感谢您对trim()的评论
method。忘记了这个例子。我相应地编辑了我的答案。我刚刚删除了我的答案,因为似乎无法很好地格式化多行代码片段。我创建了一个答案。请注意,不鼓励只使用代码的答案!注意:他们在POI Apache 3.15中不推荐使用该方法,但从版本4起,它被称为 getCellType()
再次出现。注意:POI Apache 3.15中不推荐使用该方法,但从版本4开始,它再次被称为getCellType()
。
total_colume = myRow.getLastCellNum();
int current_colume = 0;
HSSFCell ReadInCellValue;
while (current_colume <= total_colume) {
ReadInCellValue = myRow.getCell(current_colume, Row.MissingCellPolicy.CREATE_NULL_AS_BLANK);//if cell is empty, return black
if (ReadInCellValue.toString=="") Log.d("empty cell", "colume=" + String.valuOf(current_colume));
current_colume++;
}