Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -204,4 +204,12 @@ public static void afterRowDispose(RowWriteHandlerContext context) {
rowHandlerExecutionChain.afterRowDispose(context);
}
}

public static void afterSheetDispose(WriteContext writeContext) {
SheetWriteHandlerContext context = WriteHandlerUtils.createSheetWriteHandlerContext(writeContext);
SheetHandlerExecutionChain sheetHandlerExecutionChain = getSheetHandlerExecutionChain(context, false);
if (sheetHandlerExecutionChain != null) {
sheetHandlerExecutionChain.afterSheetDispose(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.idev.excel.exception.ExcelGenerateException;
import cn.idev.excel.support.ExcelTypeEnum;
import cn.idev.excel.util.FileUtils;
import cn.idev.excel.util.WriteHandlerUtils;
import cn.idev.excel.write.executor.ExcelWriteAddExecutor;
import cn.idev.excel.write.executor.ExcelWriteFillExecutor;
import cn.idev.excel.write.metadata.WriteSheet;
Expand Down Expand Up @@ -55,6 +56,8 @@ public void addContent(Collection<?> data, WriteSheet writeSheet, WriteTable wri
excelWriteAddExecutor = new ExcelWriteAddExecutor(context);
}
excelWriteAddExecutor.add(data);
// execute callback after the sheet is written
WriteHandlerUtils.afterSheetDispose(context);
} catch (RuntimeException e) {
finishOnException();
throw e;
Expand All @@ -78,6 +81,8 @@ public void fill(Object data, FillConfig fillConfig, WriteSheet writeSheet) {
excelWriteFillExecutor = new ExcelWriteFillExecutor(context);
}
excelWriteFillExecutor.fill(data, fillConfig);
// execute callback after the sheet is written
WriteHandlerUtils.afterSheetDispose(context);
} catch (RuntimeException e) {
finishOnException();
throw e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ default void afterSheetCreate(SheetWriteHandlerContext context) {
* @param writeSheetHolder
*/
default void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {}

/**
* Called after all operations on the sheet have been completed
* @param context
*/
default void afterSheetDispose(SheetWriteHandlerContext context) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,11 @@ public void addLast(SheetWriteHandler handler) {
}
context.next = new SheetHandlerExecutionChain(handler);
}

public void afterSheetDispose(SheetWriteHandlerContext context) {
this.handler.afterSheetDispose(context);
if (this.next != null) {
this.next.afterSheetDispose(context);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.idev.excel.write.handler.RowWriteHandler;
import cn.idev.excel.write.handler.SheetWriteHandler;
import cn.idev.excel.write.handler.WorkbookWriteHandler;
import cn.idev.excel.write.handler.context.SheetWriteHandlerContext;
import cn.idev.excel.write.metadata.holder.WriteSheetHolder;
import cn.idev.excel.write.metadata.holder.WriteTableHolder;
import cn.idev.excel.write.metadata.holder.WriteWorkbookHolder;
Expand All @@ -28,6 +29,7 @@ public class WriteHandler implements WorkbookWriteHandler, SheetWriteHandler, Ro
private long afterRowDispose = 0L;
private long beforeSheetCreate = 0L;
private long afterSheetCreate = 0L;
private long afterSheetDispose = 0L;
private long beforeWorkbookCreate = 0L;
private long afterWorkbookCreate = 0L;
private long afterWorkbookDispose = 0L;
Expand All @@ -51,6 +53,7 @@ public void beforeCellCreate(
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -76,6 +79,7 @@ public void afterCellCreate(
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -101,6 +105,7 @@ public void afterCellDataConverted(
Assertions.assertEquals(1L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -126,6 +131,7 @@ public void afterCellDispose(
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -150,6 +156,7 @@ public void beforeRowCreate(
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -174,6 +181,7 @@ public void afterRowCreate(
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -198,6 +206,7 @@ public void afterRowDispose(
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -216,6 +225,7 @@ public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteShee
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(0L, beforeSheetCreate);
Assertions.assertEquals(0L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -233,6 +243,7 @@ public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheet
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(0L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -250,6 +261,7 @@ public void beforeWorkbookCreate() {
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(0L, beforeSheetCreate);
Assertions.assertEquals(0L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(0L, beforeWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -267,6 +279,7 @@ public void afterWorkbookCreate(WriteWorkbookHolder writeWorkbookHolder) {
Assertions.assertEquals(0L, afterRowDispose);
Assertions.assertEquals(0L, beforeSheetCreate);
Assertions.assertEquals(0L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
Expand All @@ -284,12 +297,31 @@ public void afterWorkbookDispose(WriteWorkbookHolder writeWorkbookHolder) {
Assertions.assertEquals(1L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(1L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
afterWorkbookDispose++;
}

@Override
public void afterSheetDispose(SheetWriteHandlerContext context) {
Assertions.assertEquals(1L, beforeCellCreate);
Assertions.assertEquals(1L, afterCellCreate);
Assertions.assertEquals(1L, afterCellDataConverted);
Assertions.assertEquals(1L, afterCellDispose);
Assertions.assertEquals(1L, beforeRowCreate);
Assertions.assertEquals(1L, afterRowCreate);
Assertions.assertEquals(1L, afterRowDispose);
Assertions.assertEquals(1L, beforeSheetCreate);
Assertions.assertEquals(1L, afterSheetCreate);
Assertions.assertEquals(0L, afterSheetDispose);
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(0L, afterWorkbookDispose);
afterSheetDispose++;
}

public void afterAll() {
Assertions.assertEquals(1L, beforeCellCreate);
Assertions.assertEquals(1L, afterCellCreate);
Expand All @@ -303,5 +335,6 @@ public void afterAll() {
Assertions.assertEquals(1L, beforeWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookCreate);
Assertions.assertEquals(1L, afterWorkbookDispose);
Assertions.assertEquals(1L, afterSheetDispose);
}
}
25 changes: 25 additions & 0 deletions fastexcel/src/test/java/cn/idev/excel/test/demo/fill/FillTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import cn.idev.excel.test.util.TestFileUtil;
import cn.idev.excel.util.ListUtils;
import cn.idev.excel.util.MapUtils;
import cn.idev.excel.write.handler.SheetWriteHandler;
import cn.idev.excel.write.handler.context.SheetWriteHandlerContext;
import cn.idev.excel.write.metadata.WriteSheet;
import cn.idev.excel.write.metadata.fill.FillConfig;
import cn.idev.excel.write.metadata.fill.FillWrapper;
Expand All @@ -14,6 +16,8 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.junit.jupiter.api.Test;

/**
Expand Down Expand Up @@ -260,6 +264,27 @@ public void dateFormatFill() {
EasyExcel.write(fileName).withTemplate(templateFileName).sheet().doFill(data());
}

@Test
public void testFillSheetDispose() {
String templateFileName =
TestFileUtil.getPath() + "demo" + File.separator + "fill" + File.separator + "simple.xlsx";
String fileName = TestFileUtil.getPath() + "simpleFill" + System.currentTimeMillis() + ".xlsx";
FillData fillData = new FillData();
fillData.setName("Zhang San");
fillData.setNumber(5.2);
EasyExcel.write(fileName)
.withTemplate(templateFileName)
.sheet()
.registerWriteHandler(new SheetWriteHandler() {
@Override
public void afterSheetDispose(SheetWriteHandlerContext context) {
Sheet sheet = context.getWriteSheetHolder().getSheet();
sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 2, 0, 1));
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers (1, 2, 0, 1) should be replaced with named constants or variables to improve code readability and maintainability.

Suggested change
sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 2, 0, 1));
sheet.addMergedRegionUnsafe(
new CellRangeAddress(
MERGE_FIRST_ROW,
MERGE_LAST_ROW,
MERGE_FIRST_COL,
MERGE_LAST_COL
)
);

Copilot uses AI. Check for mistakes.
}
})
.doFill(fillData);
}

private List<FillData> data() {
List<FillData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import cn.idev.excel.util.ListUtils;
import cn.idev.excel.write.handler.CellWriteHandler;
import cn.idev.excel.write.handler.EscapeHexCellWriteHandler;
import cn.idev.excel.write.handler.SheetWriteHandler;
import cn.idev.excel.write.handler.context.CellWriteHandlerContext;
import cn.idev.excel.write.handler.context.SheetWriteHandlerContext;
import cn.idev.excel.write.merge.LoopMergeStrategy;
import cn.idev.excel.write.metadata.WriteSheet;
import cn.idev.excel.write.metadata.WriteTable;
Expand All @@ -38,11 +40,13 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.poi.ss.usermodel.*;
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using wildcard imports is discouraged as it makes dependencies unclear and can lead to naming conflicts. Import specific classes instead of using '*'.

Suggested change
import org.apache.poi.ss.usermodel.*;

Copilot uses AI. Check for mistakes.
import org.apache.poi.ss.usermodel.Cell;
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is redundant since Cell is already imported via the wildcard import on line 43. Remove this duplicate import.

Suggested change
import org.apache.poi.ss.usermodel.Cell;

Copilot uses AI. Check for mistakes.
import org.apache.poi.ss.usermodel.CellStyle;
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is redundant since CellStyle is already imported via the wildcard import on line 43. Remove this duplicate import.

Suggested change
import org.apache.poi.ss.usermodel.CellStyle;

Copilot uses AI. Check for mistakes.
import org.apache.poi.ss.usermodel.FillPatternType;
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is redundant since FillPatternType is already imported via the wildcard import on line 43. Remove this duplicate import.

Suggested change
import org.apache.poi.ss.usermodel.FillPatternType;

Copilot uses AI. Check for mistakes.
import org.apache.poi.ss.usermodel.IndexedColors;
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is redundant since IndexedColors is already imported via the wildcard import on line 43. Remove this duplicate import.

Suggested change
import org.apache.poi.ss.usermodel.IndexedColors;

Copilot uses AI. Check for mistakes.
import org.apache.poi.ss.usermodel.Workbook;
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import is redundant since Workbook is already imported via the wildcard import on line 43. Remove this duplicate import.

Suggested change
import org.apache.poi.ss.usermodel.Workbook;

Copilot uses AI. Check for mistakes.
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -741,6 +745,23 @@ public void noModelWrite() {
EasyExcel.write(fileName).head(head()).sheet("模板").doWrite(dataList());
}

@Test
public void sheetDisposeTest() {
String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
EasyExcel.write(fileName, DemoData.class)
.sheet("模板")
.registerWriteHandler(new SheetWriteHandler() {
@Override
public void afterSheetDispose(SheetWriteHandlerContext context) {
Sheet sheet = context.getWriteSheetHolder().getSheet();
// 合并区域单元格
sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 10, 2, 2));
Copy link

Copilot AI Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The magic numbers (1, 10, 2, 2) should be replaced with named constants or variables to improve code readability and maintainability.

Suggested change
sheet.addMergedRegionUnsafe(new CellRangeAddress(1, 10, 2, 2));
sheet.addMergedRegionUnsafe(new CellRangeAddress(MERGE_FIRST_ROW, MERGE_LAST_ROW, MERGE_FIRST_COL, MERGE_LAST_COL));

Copilot uses AI. Check for mistakes.
}
})
.doWrite(this::data);
System.out.println(fileName);
}

private List<LongestMatchColumnWidthData> dataLong() {
List<LongestMatchColumnWidthData> list = ListUtils.newArrayList();
for (int i = 0; i < 10; i++) {
Expand Down