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();
相关推荐
light blue bird3 分钟前
主子端台二分法任务汇总组件
前端·数据库·.net·桌面端winform
jeffwang1 小时前
我做了个让 AI 看屏幕跑测试的工具,因为 Playwright 测不了我的 Flutter Web
前端
HSunR2 小时前
dify 搭建ai作业批改流
开发语言·前端·javascript
代码不加糖2 小时前
2026 跨境电商独立站实战:从 0 到 1 搭建高转化 SaaS 商城(附源码)
开发语言·前端·javascript
亲亲小宝宝鸭2 小时前
拖一拖控件,拖出个问卷(低代码平台)
前端·低代码
江南十四行2 小时前
ReAct Agent 基本理论与项目实战(一)
前端·react.js·前端框架
We་ct3 小时前
LeetCode 72. 编辑距离:动态规划经典题解
前端·算法·leetcode·typescript·动态规划
小呆呆6663 小时前
Codex 穷鬼大救星
前端·人工智能·后端
当时只道寻常3 小时前
Vue3 + IntersectionObserver 实现高性能图片懒加载
前端
sakiko_4 小时前
UIKit学习笔记3-布局、滚动视图、隐藏或显示视图
前端·笔记·学习·objective-c·swift·uikit