Skip to content

Commit

Permalink
bugfix: ExcelUtils,当表格为空时,出现死循环的问题修复。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jun 21, 2024
1 parent 2032a28 commit 32f7b1f
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ public abstract class ExcelUtils {
* @throws Exception 异常
*/
public static <T> List<T> toList(Workbook book, Class<T> clazz, Predicate<T> validDataFun) throws Exception {
Sheet sheet = book.getSheetAt(0);
if (sheet == null || sheet.getPhysicalNumberOfRows() == 0) {
Sheet sheet = getHasDataSheet(book);
if (sheet == null) {
return new ArrayList<>(); // 没有数据,直接返回空
}

Expand Down Expand Up @@ -346,7 +346,7 @@ public static Class<?> getClassFromMap(Map<String, List<?>> dataMap) {
* 数据转换为excel
*
* @param dataMap 数据列表
* @param clazz 数据类
* @param clazz 数据类
* @return wbk 返回excel文件流
*/
public static Workbook toExcel(Map<String, List<?>> dataMap, Class<?> clazz) {
Expand Down Expand Up @@ -393,6 +393,28 @@ public static Workbook toExcel(Map<String, List<?>> dataMap, Class<?> clazz) {
return book;
}


//endregion


/**
* 获取有数据的Sheet
*
* @param book 工作簿
* @return 有数据的Sheet
*/
@Nullable
public static Sheet getHasDataSheet(Workbook book) {
if (book == null) {
return null;
}

for (int i = 0; i < book.getNumberOfSheets(); i++) {
Sheet sheet = book.getSheetAt(i);
if (sheet.getPhysicalNumberOfRows() > 0) {
return sheet;
}
}

return null;
}
}

0 comments on commit 32f7b1f

Please sign in to comment.