修复Java使用POI合并Excel单元格后,边框不显示的问题

时间:2021-10-15 19:21:38

使用Apache POI生成Excel文档时,当进行单元格合并操作后,被合并的单元格边框会消失,使用如下方式可以解决。

  • 创建方法:
        public void setBorderStyle(int border, CellRangeAddress region, HSSFSheet sheet, HSSFWorkbook wb){
    RegionUtil.setBorderBottom(border, region, sheet, wb);  //下边框
    RegionUtil.setBorderLeft(border, region, sheet, wb); //左边框
    RegionUtil.setBorderRight(border, region, sheet, wb); //右边框
    RegionUtil.setBorderTop(border, region, sheet, wb); //上边框
    }
  • 调用方式:
        //创建wb,sheet的过程略过
    CellRangeAddress region = new CellRangeAddress(0, 0, i, i+1); //给定要合并的单元格范围
    sheet.addMergedRegion(region); //这是合并单元格过程
    setBorderStyle(HSSFCellStyle.BORDER_THIN, region, sheet, wb); //给合并过的单元格加边框