Apache Poi 实现导出excel表格 合并区域边框未完全显示的问题

1、创建行及对应单元格,并合并行或列,其中cellStyle设置边框投屏top、bottom、left、right;

复制代码
Row row=sheet.createRow(rowIndex);

Cell cell=row.createCell(colIndex);
cell.setCellValue("测试");
cell.setCellStyle(cellStyle);

CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
sheet.addMergedRegion(region);

此时导出的excel表格样式显示如图。

2、解决方法:采用RegionUtil设置合并区域的边框

复制代码
CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
sheet.addMergedRegion(region);

//合并区域添加边框
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);

理论上采用上述方式设置合并区域的边框是可用的,但发现导出的表格合并区域还是缺少边框。表格显示如图。

将已合并的单元格手动取消合并后,发现边框均存在,再次手动合并单元格发现无边框缺失。

3、经过测试发现,是因为本项目在表格渲染的时候,将前六列与后续列分开渲染的,并且在后续列的渲染中接着新建行,没有延用前六列新建的行,于是产生了上述的问题。修改后合并区域的边框显示完整。

如有相同问题的小伙伴,可参考本文解决方式。

相关推荐
萧瑟余晖16 分钟前
Spring Cloud微服务总览与注册发现详解
spring·spring cloud·微服务
betazhou3 小时前
Apache Seatunnel 抽取SQL server实时同步到Oracle(四)
数据库·oracle·apache·sql server·seatunnel
ProcessOn官方账号4 小时前
ProcessOn Office 插件上线:在 Word / Excel / PPT 里直接画流程图
word·excel·插件·office·processon
七夜zippoe1 天前
Spring AI 是什么?Java 工程师的 AI 开发第一站
java·人工智能·spring·ai·开发
wangjialelele1 天前
一文梳理 Spring Cloud 核心组件:Eureka、Nacos、OpenFeign、LoadBalancer 与 Gateway
java·spring boot·spring·spring cloud·微服务·nacos
萧瑟余晖1 天前
Spring Security综合实战与安全加固详解
java·安全·spring
CarIise1 天前
Spring MVC 请求参数绑定与常用注解2
java·spring·mvc
Nebula_g1 天前
SpringBoot企业级实战项目—云禾外卖—SpringBoot常用注解解析
java·网络·数据库·spring boot·后端·spring·javase
CarIise1 天前
Spring MVC 请求参数绑定与常用注解
java·spring·mvc
萧瑟余晖1 天前
Spring Security 6.x新特性与架构演进详解
java·spring·架构