From 26e6bbcb6acdb7a87dff23b5546c06109a228bd0 Mon Sep 17 00:00:00 2001 From: Ricky Date: Mon, 1 Feb 2021 10:21:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96Excel=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=A9=BA=E8=A1=8C=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/poi/ExcelUtil.java | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java index 2a0e3b216..49c59cbbb 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/ExcelUtil.java @@ -185,7 +185,8 @@ public class ExcelUtil 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 { // 从第2行开始取数据,默认第一行是表头. Row row = sheet.getRow(i); + // 判断当前行是否是空行 + if (isRowEmpty(row)) + { + continue; + } T entity = null; for (Map.Entry entry : fieldsMap.entrySet()) { @@ -1090,4 +1096,27 @@ public class ExcelUtil } 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; + } } \ No newline at end of file