Skip to content

Commit

Permalink
bugfix: 使用低版本POI时,导出excel功能头行会多出一个空行的问题修复。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jun 7, 2024
1 parent f592a7f commit 51a7681
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected void createCustomFooterRow(Map<Object, Object> 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);
// 合并单元格
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; // 标记当前列号

// 创建序号列头
Expand Down Expand Up @@ -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; // 序号

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ public static <T> List<T> toList(Workbook book, Class<T> clazz, Predicate<T> 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++; // 过滤起始的空行
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
// 创建数据行
Expand All @@ -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 {
Expand Down

0 comments on commit 51a7681

Please sign in to comment.