EasyExcel使用模版填充的方式,导致单元格边框消失

1、源代码,使用writer.fill()这种方式,生成的excel发现最下边的边框消失,我的模版最下边的是厚实线,导出后,变成了细实线

复制代码
ExcelWriter writer = EasyExcel.write(response.getOutputStream())
        .registerWriteHandler(customStyleHandler)
        .withTemplate(getInputStream("Y4-06.xlsx"))
        .build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
writer.fill(data, writeSheet);
writer.fill(dataList, writeSheet);
writer.finish();

2、解决办法

第一种:如果你的数据载体是对象,那么你可以直接在对象的实体上加上一下注解 ,但是这种的会让你导出的每一行的底边框全部为注解设置的样式。

复制代码
@ContentStyle(borderBottom = BorderStyleEnum.THICK)

第二种:增加自定义样式处理器,直接上代码

复制代码
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.context.CellWriteHandlerContext;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Workbook;

public class CustomCellStyleHandler implements CellWriteHandler {
    @Override
    public void afterCellDispose(CellWriteHandlerContext context) {
        if (context.getHead()) return; // 跳过表头

        Workbook workbook = context.getWriteWorkbookHolder().getWorkbook();

        if (context.getCell().getRowIndex() == 15) {
            CellStyle style = workbook.createCellStyle();
            style.cloneStyleFrom(context.getCell().getCellStyle());

            WriteCellStyle writeCellStyle = new WriteCellStyle();
            writeCellStyle.setBorderBottom(BorderStyle.THICK);
            context.getCellDataList().get(0).setWriteCellStyle(writeCellStyle);
        }
    }
}

以上代码有几个点要注意:

接口类CellWriteHandler

重写方法afterCellDispose

最为重点的是context.getCellDataList().get(0).setWriteCellStyle(writeCellStyle),一定要用WriteCellStyle来设置你的样式,我看网上有的用CellStyle,不起作用,这个我查了下,WriteCellStyle是EasyExcel自己的,CellStyle是POI的,不知道具体什么个原因。

复制代码
CellWriteHandler customStyleHandler = new CustomCellStyleHandler();
ExcelWriter writer = EasyExcel.write(response.getOutputStream())
        .registerWriteHandler(customStyleHandler)
        .withTemplate(getInputStream("Y4-06.xlsx"))
        .build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
writer.fill(data, writeSheet);
writer.fill(dataList, writeSheet);
writer.finish();
相关推荐
JNX_SEMI14 分钟前
AT2401C 2.4GHz 全集成射频前端单芯片技术解析
前端·单片机·嵌入式硬件·物联网·硬件工程
anOnion32 分钟前
Agentic 前端开发之 实时显示 AI Agent 终端输出
前端·javascript·人工智能
随风一样自由42 分钟前
【前端领域】2026最新前端领域全梳理(框架/工具/AI/跨端/底层标准/就业趋势)
前端·人工智能·前端框架
这是个栗子42 分钟前
【前端性能优化】优化数据加载:用 Promise.all 从串行到并行
前端·javascript·性能优化·异步编程·前端优化·promise.all
fei_sun1 小时前
黑洞路由(Null Route/空接口路由)
服务器·前端·javascript
大爱一家盟2 小时前
告别卡点BGM同质化 2026原创卡点音乐素材下载网站 TOP5 推荐
大数据·前端·人工智能
彦为君2 小时前
算法思维与经典智力题
java·前端·redis·算法
aa小小2 小时前
localhost 访问异常排查笔记
前端
格子软件2 小时前
2026年GEO优化系统源码的分布式状态机深度拆解
java·前端·vue.js·vue·geo
陈随易3 小时前
Rust、Golang、MoonBit 编译成 WASM,体积和速度差距有多大?
前端·后端·程序员