Skip to content

Commit 51a7681

Browse files
bugfix: 使用低版本POI时,导出excel功能头行会多出一个空行的问题修复。
1 parent f592a7f commit 51a7681

File tree

3 files changed

+10
-10
lines changed

3 files changed

+10
-10
lines changed

core-parent/poi/src/main/java/icu/easyj/poi/excel/hook/AbstractListToExcelHook.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected void createCustomFooterRow(Map<Object, Object> context, Sheet sheet, E
121121
}
122122

123123
// 创建行及单元格
124-
Row row = sheet.createRow(sheet.getLastRowNum() + 1);
124+
Row row = sheet.createRow(sheet.getPhysicalNumberOfRows());
125125
Cell cell = row.createCell(0);
126126
cell.setCellValue(footerRowContent);
127127
// 合并单元格

core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelRowUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public static void createHeadRow(Sheet sheet, ExcelMapping mapping) {
230230
}
231231

232232
// 创建行
233-
Row row = sheet.createRow(sheet.getLastRowNum() + 1);
233+
Row row = sheet.createRow(sheet.getPhysicalNumberOfRows());
234234
int cellNum = 0; // 标记当前列号
235235

236236
// 创建序号列头
@@ -286,7 +286,7 @@ public static void createHeadRow(Sheet sheet, ExcelMapping mapping) {
286286
* @param mapping 表格映射
287287
*/
288288
public static void createDataRows(Sheet sheet, List<?> dataList, ExcelMapping mapping) {
289-
int rowNum = sheet.getLastRowNum() + 1; // 开始行号
289+
int rowNum = sheet.getPhysicalNumberOfRows(); // 开始行号
290290
int cellNum; // 当前列号
291291
int number = 1; // 序号
292292

core-parent/poi/src/main/java/icu/easyj/poi/excel/util/ExcelUtils.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ public static <T> List<T> toList(Workbook book, Class<T> clazz, Predicate<T> val
6666
// 获取映射
6767
ExcelMapping mapping = ExcelMapping.getMapping(clazz);
6868

69-
// 获取数据实际的起始行号
69+
// 获取数据实际的起止行号
7070
int rowStart = sheet.getFirstRowNum();
71-
int rowEnd = sheet.getLastRowNum();
71+
int rowEnd = sheet.getPhysicalNumberOfRows() - 1;
7272
while (ExcelRowUtils.isEmptyRow(sheet.getRow(rowStart))) {
7373
rowStart++; // 过滤起始的空行
7474
}
@@ -179,7 +179,7 @@ public static boolean getHasNumberCell(Sheet sheet, ExcelMapping mapping) {
179179
Row row;
180180
Cell cell;
181181
Object cellValue;
182-
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
182+
for (int i = 0; i <= sheet.getPhysicalNumberOfRows() - 1; i++) {
183183
row = sheet.getRow(i);
184184
if (ExcelRowUtils.isEmptyRow(row)) {
185185
continue;
@@ -209,9 +209,9 @@ public static boolean getHasNumberCell(Sheet sheet, ExcelMapping mapping) {
209209
*/
210210
@Nullable
211211
private static Integer findHeadRowNum(Sheet sheet, int firstRowNum, ExcelMapping mapping) {
212-
// 只检测5行
212+
// 只检测前3行
213213
int i = 0;
214-
while (i < 5) {
214+
while (i < 3) {
215215
Row row = sheet.getRow(firstRowNum + i);
216216
if (row != null && ExcelRowUtils.isHeadRow(row, mapping)) {
217217
return row.getRowNum();
@@ -268,7 +268,7 @@ public static Workbook toExcel(List<?> dataList, Class<?> clazz) {
268268
ListToExcelHookTrigger.onBeforeCreateHeadRow(sheet, mapping);
269269

270270
// 除去自定义行以外的首行
271-
int firstRowNum = sheet.getLastRowNum() + 1;
271+
int firstRowNum = sheet.getPhysicalNumberOfRows();
272272
// 创建头行
273273
ExcelRowUtils.createHeadRow(sheet, mapping);
274274
// 创建数据行
@@ -277,7 +277,7 @@ public static Workbook toExcel(List<?> dataList, Class<?> clazz) {
277277
// 触发勾子:afterCreateDataRows
278278
ListToExcelHookTrigger.onAfterCreateDataRows(sheet, mapping);
279279

280-
// 写文件后,设置样式:如自适应宽度等
280+
// 表格内容填充完后,设置样式:如自适应宽度等
281281
ExcelCellUtils.setCellStyle(sheet, mapping, firstRowNum, false);
282282
} catch (Exception e) {
283283
try {

0 commit comments

Comments
 (0)