优化Excel导入增加空行判断

pull/262/head
Ricky 2021-02-01 10:21:34 +08:00
parent 3d12a776bc
commit 26e6bbcb6a
1 changed files with 30 additions and 1 deletions

View File

@ -185,7 +185,8 @@ public class ExcelUtil<T>
throw new IOException("文件sheet不存在");
}
int rows = sheet.getPhysicalNumberOfRows();
// 获取最后一个非空行的行下标比如总行数为n则返回的为n-1
int rows = sheet.getLastRowNum();
if (rows > 0)
{
@ -229,6 +230,11 @@ public class ExcelUtil<T>
{
// 从第2行开始取数据,默认第一行是表头.
Row row = sheet.getRow(i);
// 判断当前行是否是空行
if (isRowEmpty(row))
{
continue;
}
T entity = null;
for (Map.Entry<Integer, Field> entry : fieldsMap.entrySet())
{
@ -1090,4 +1096,27 @@ public class ExcelUtil<T>
}
return val;
}
/**
*
*
* @param row
* @return
*/
private boolean isRowEmpty(Row row)
{
if (row == null)
{
return true;
}
for (int i = row.getFirstCellNum(); i < row.getLastCellNum(); i++)
{
Cell cell = row.getCell(i);
if (cell != null && cell.getCellType() != CellType.BLANK)
{
return false;
}
}
return true;
}
}