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

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

相关推荐
CodeToGym20 小时前
【Java 办公自动化】Apache POI 入门:手把手教你实现 Excel 导入与导出
java·apache·excel
张3蜂21 小时前
深入理解 Python 的 frozenset:为什么要有“不可变集合”?
前端·python·spring
Coder_Boy_1 天前
基于Spring AI的分布式在线考试系统-事件处理架构实现方案
人工智能·spring boot·分布式·spring
7哥♡ۣۖᝰꫛꫀꪝۣℋ1 天前
Spring-cloud\Eureka
java·spring·微服务·eureka
一灰灰blog1 天前
Spring AI中的多轮对话艺术:让大模型主动提问获取明确需求
数据库·人工智能·spring
Java水解1 天前
【JAVA 进阶】Spring AOP核心原理:JDK与CGLib动态代理实战解析
后端·spring
码云数智-大飞1 天前
保姆级教程:零基础快速上手 Apache SeaTunnel(原 Waterdrop)
apache
暮色妖娆丶1 天前
Spring 源码分析 BeanFactoryPostProcessor
spring boot·spring·源码
yuluo_YX1 天前
Reactive 编程 - Java Reactor
java·python·apache
暮色妖娆丶1 天前
SpringBoot 启动流程源码分析 ~ 它其实不复杂
spring boot·后端·spring