多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
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); } ```