使用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); // 右边框
文章评论