如何使用 Apache POI 确定 .xls 文档中的空行?
我在我的 POI 项目中使用了以下方法,效果很好。这是泽勒解决方案的变体。
public static boolean isRowEmpty(Row row) {
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK)
return false;
}
return true;
}
行迭代器仅返回包含数据的行,但是如果它们完全为空,则按行索引迭代,
getRow(index)
返回null
解决方案:
直至 POI 版本 3.14(感谢 Sergii Lisnychyi):
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}
从 POI 版本 3.15 到 4.2(
int getCellType()
已弃用):
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}
从 POI 版本 4 开始(
CellTypeEnum getCellTypeEnum()
将返回 Enum 而不是 int):
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellTypeEnum() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}
从 POI 版本 5.1 开始(CellTypeEnum getCellTypeEnum() 重命名为 getCellType()):
private boolean checkIfRowIsEmpty(Row row) {
if (row == null) {
return true;
}
if (row.getLastCellNum() <= 0) {
return true;
}
for (int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++) {
Cell cell = row.getCell(cellNum);
if (cell != null && cell.getCellType() != CellType.BLANK && StringUtils.isNotBlank(cell.toString())) {
return false;
}
}
return true;
}
您必须迭代该行中的所有单元格并检查它们是否全部为空。我不知道还有其他解决方案...
short c;
for (c = lastRow.getFirstCellNum(); c <= lastRow.getLastCellNum(); c++) {
cell = lastRow.getCell(c);
if (cell != null && lastRow.getCell(c).getCellType() != HSSFCell.CELL_TYPE_BLANK) {
nonBlankRowFound = true;
}
}
代码来自这里
假设您想检查第
n
行是否为空,请记住 Apache POI 中的行是从零开始而不是从一开始,您需要类似以下内容:
Row r = sheet.getRow(n-1); // 2nd row = row 1
boolean hasData = true;
if (r == null) {
// Row has never been used
hasData = false;
} else {
// Check to see if all cells in the row are blank (empty)
hasData = false;
for (Cell c : r) {
if (c.getCellType() != Cell.CELL_TYPE_BLANK) {
hasData = true;
break;
}
}
}
是的,但是如果在某些行中,我们将在某些单元格 = “”中拥有某些单元格,并且在其他单元格中拥有空值。 这个方法效果会更好:
boolean isEmptyRow(Row row){
boolean isEmptyRow = true;
for(int cellNum = row.getFirstCellNum(); cellNum < row.getLastCellNum(); cellNum++){
Cell cell = row.getCell(cellNum);
if(cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK && StringUtils.isNotBlank(cell.toString())){
isEmptyRow = false;
}
}
return isEmptyRow;
}
如果您使用 apache-poi [4+]:
那么下面的方法适合你。由于建议的其他方法对我不起作用,我不得不这样做。
public static boolean isRowEmpty(Row row) {
boolean isEmpty = true;
DataFormatter dataFormatter = new DataFormatter();
if(row != null) {
for(Cell cell: row) {
if(dataFormatter.formatCellValue(cell).trim().length() > 0) {
isEmpty = false;
break;
}
}
}
return isEmpty;
}
方法
dataFormatter.formatCellValue(cell)
将返回 ""
,当单元格为 empty / ZERO length string
或 null
时,会返回 BLANK
。
进口声明供您参考:
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
希望这有帮助!
boolean isEmptyRow(Row row) {
boolean isEmpty=true;
String data="";
for(Cell cell:row) {
data=data.concat(cell.getStringCellValue());
}
if(!data.trim().isEmpty()) {
isEmpty=false;
}
return isEmpty;
}
尝试使用 if(iterator.hasNext)
Row nextRow = null;
Cell nextCell = null;
Iterator<Row> iterator = firstSheet.rowIterator();
if(iterator.hasNext) {
return true;
}
else {
return false;
}
获取 CellReference 的实例并在该实例上使用 formatAsString() 。将其与空字符串进行比较
`
if("".equals(cellRef.formatAsString())){
System.out.println("this is an empty cell");
}else{
System.out.println("Cell value : "+cellRef.formatAsString());
}
` 参考:http://www.javabeat.net/2007/10/apache-poi-reading-excel-sheet-using-java/
Row == null 在我的例子中是有效的。
int total_row = mySheet.getLastRowNum();
int current_row = 0;
HSSFRow hssf_Row;
while (current_row <= total_row) {
hssf_Row = mySheet.getRow(current_row);
if (hssf_Row == null) Log.d("empty row","row=" + String.valueOf(current_row));
current_row++;
}
您还可以使用
row.cellIterator()
方法,该方法返回包含所有单元格的行的迭代器。
如果该行为空
Iterators.size(row.cellIterator())
将为零
至少从
poi-ooxm 5.2.3
开始,你似乎可以做到:
for (Row row : sheet) {
if (row.getLastCellNum() == -1) {
continue;
}
//do processing
}
这不会检查空文本等,您可能需要@EpicPandaForce 的稳健答案。 但对于您团队中的某人添加了一堆空白行的情况,这似乎就是您所需要的。