解决ApachePOI单元格合并后,边框不见的问题

2022-12-25 312点热度 0人点赞 0条评论

使用Apache POI生成单元格内容,给单元格设置了边框,代码如下:

// 创建带有四个变量的CellStyle
CellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(BorderStyle.THIN);
cellStyle.setBorderLeft(BorderStyle.THIN);
cellStyle.setBorderRight(BorderStyle.THIN);
cellStyle.setBorderTop(BorderStyle.THIN);

Cell cell = headerRow.createCell(0);
cell.setCellStyle(cellStyle); // 给cell设置带有边框的cellStyle

不过有个问题,在合并了一些单元格之后,原来设置了边框的单元格合并后,就没有边框了。

解决方法是合并后,使用 RegionUtil.setBorde[Top|Bottom|Left|Right] 给合并后的单元格重新设置4个边框,代码如下:

sheet.addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); // 合并单元格,四个参数分别是开始行、结束行、开始列、结束列

RegionUtil.setBorderTop(BorderStyle.THIN, new CellRangeAddress(firstRow, lastRow, firstCol, lastCol), sheet); // 上边框
RegionUtil.setBorderBottom(BorderStyle.THIN, new CellRangeAddress(firstRow, lastRow, firstCol, lastCol), sheet); // 下边框
RegionUtil.setBorderLeft(BorderStyle.THIN, new CellRangeAddress(firstRow, lastRow, firstCol, lastCol), sheet); // 左边框
RegionUtil.setBorderRight(BorderStyle.THIN, new CellRangeAddress(firstRow, lastRow, firstCol, lastCol), sheet); // 右边框

 

admin

这个人很懒,什么都没留下

文章评论

您需要 登录 之后才可以评论