企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
EasyExcel 不仅提供了一些简单使用的功能,还提供了写拦截器,当这些简单的功能不满足要求时,便可以调用拦截器来完成。 ```java /** 共有 4 种类型拦截器,每种拦截器都有很多的抽象实现类 */ public interface CellWriteHandler extends WriteHandler { ... } public interface RowWriteHandler extends WriteHandler { ... } public interface SheetWriteHandler extends WriteHandler { ... } public interface WorkbookWriteHandler extends WriteHandler { ... } ``` 下面通过使用 SheetWriteHandler 拦截器实现冻结表头来讲解拦截器的使用步骤,其它的都是一样的道理。 **1. 实现拦截器接口** ```java public class CustomSheetWriteHandler implements SheetWriteHandler { @Override public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) { Sheet sheet = writeSheetHolder.getSheet(); //冻结前0列,前1行 sheet.createFreezePane(0, 1); } } ``` **2. 注册拦截器即可** ```java @Test public void write01() { Integer num = 0; List<List<Integer>> dataList = Lists.newArrayList(); for (int row = 1; row <= 100; row++) { List<Integer> rowData = Lists.newArrayList(); for (int col = 1; col <= 50; col++) { rowData.add(num++); } dataList.add(rowData); } EasyExcel.write("e:/upload/easyexcel/003.xlsx") .sheet("Sheet1") //注册拦截器 .registerWriteHandler(new CustomSheetWriteHandler()) .doWrite(dataList); } ```