From 51a76814f89c9fa51a9cf1cab1bcd3706f9d503d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Fri, 7 Jun 2024 14:07:04 +0800 Subject: [PATCH] =?UTF-8?q?bugfix:=20=E4=BD=BF=E7=94=A8=E4=BD=8E=E7=89=88?= =?UTF-8?q?=E6=9C=ACPOI=E6=97=B6=EF=BC=8C=E5=AF=BC=E5=87=BAexcel=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=E5=A4=B4=E8=A1=8C=E4=BC=9A=E5=A4=9A=E5=87=BA=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E7=A9=BA=E8=A1=8C=E7=9A=84=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../poi/excel/hook/AbstractListToExcelHook.java | 2 +- .../icu/easyj/poi/excel/util/ExcelRowUtils.java | 4 ++-- .../java/icu/easyj/poi/excel/util/ExcelUtils.java | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core-parent/poi/src/main/java/icu/easyj/poi/excel/hook/AbstractListToExcelHook.java b/core-parent/poi/src/main/java/icu/easyj/poi/excel/hook/AbstractListToExcelHook.java index 37975d4e..da236285 100644 --- a/core-parent/poi/src/main/java/icu/easyj/poi/excel/hook/AbstractListToExcelHook.java +++ b/core-parent/poi/src/main/java/icu/easyj/poi/excel/hook/AbstractListToExcelHook.java @@ -121,7 +121,7 @@ protected void createCustomFooterRow(Map context, Sheet sheet, E } // 创建行及单元格 - Row row = sheet.createRow(sheet.getLastRowNum() + 1); + Row row = sheet.createRow(sheet.getPhysicalNumberOfRows()); Cell cell = row.createCell(0); cell.setCellValue(footerRowContent); // 合并单元格 diff --git a/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelRowUtils.java b/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelRowUtils.java index a16b8be6..f18ad35f 100644 --- a/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelRowUtils.java +++ b/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelRowUtils.java @@ -230,7 +230,7 @@ public static void createHeadRow(Sheet sheet, ExcelMapping mapping) { } // 创建行 - Row row = sheet.createRow(sheet.getLastRowNum() + 1); + Row row = sheet.createRow(sheet.getPhysicalNumberOfRows()); int cellNum = 0; // 标记当前列号 // 创建序号列头 @@ -286,7 +286,7 @@ public static void createHeadRow(Sheet sheet, ExcelMapping mapping) { * @param mapping 表格映射 */ public static void createDataRows(Sheet sheet, List dataList, ExcelMapping mapping) { - int rowNum = sheet.getLastRowNum() + 1; // 开始行号 + int rowNum = sheet.getPhysicalNumberOfRows(); // 开始行号 int cellNum; // 当前列号 int number = 1; // 序号 diff --git a/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelUtils.java b/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelUtils.java index 2a9cecb8..67856b46 100644 --- a/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelUtils.java +++ b/core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelUtils.java @@ -66,9 +66,9 @@ public static List toList(Workbook book, Class clazz, Predicate val // 获取映射 ExcelMapping mapping = ExcelMapping.getMapping(clazz); - // 获取数据实际的起始行号 + // 获取数据实际的起止行号 int rowStart = sheet.getFirstRowNum(); - int rowEnd = sheet.getLastRowNum(); + int rowEnd = sheet.getPhysicalNumberOfRows() - 1; while (ExcelRowUtils.isEmptyRow(sheet.getRow(rowStart))) { rowStart++; // 过滤起始的空行 } @@ -179,7 +179,7 @@ public static boolean getHasNumberCell(Sheet sheet, ExcelMapping mapping) { Row row; Cell cell; Object cellValue; - for (int i = 0; i <= sheet.getLastRowNum(); i++) { + for (int i = 0; i <= sheet.getPhysicalNumberOfRows() - 1; i++) { row = sheet.getRow(i); if (ExcelRowUtils.isEmptyRow(row)) { continue; @@ -209,9 +209,9 @@ public static boolean getHasNumberCell(Sheet sheet, ExcelMapping mapping) { */ @Nullable private static Integer findHeadRowNum(Sheet sheet, int firstRowNum, ExcelMapping mapping) { - // 只检测5行 + // 只检测前3行 int i = 0; - while (i < 5) { + while (i < 3) { Row row = sheet.getRow(firstRowNum + i); if (row != null && ExcelRowUtils.isHeadRow(row, mapping)) { return row.getRowNum(); @@ -268,7 +268,7 @@ public static Workbook toExcel(List dataList, Class clazz) { ListToExcelHookTrigger.onBeforeCreateHeadRow(sheet, mapping); // 除去自定义行以外的首行 - int firstRowNum = sheet.getLastRowNum() + 1; + int firstRowNum = sheet.getPhysicalNumberOfRows(); // 创建头行 ExcelRowUtils.createHeadRow(sheet, mapping); // 创建数据行 @@ -277,7 +277,7 @@ public static Workbook toExcel(List dataList, Class clazz) { // 触发勾子:afterCreateDataRows ListToExcelHookTrigger.onAfterCreateDataRows(sheet, mapping); - // 写文件后,设置样式:如自适应宽度等 + // 表格内容填充完后,设置样式:如自适应宽度等 ExcelCellUtils.setCellStyle(sheet, mapping, firstRowNum, false); } catch (Exception e) { try {