Adding border to a merged region in POI XSSF workbook

@Jesanagua just saved my life, I just had to change a bit to match 3.17.

private void setBordersToMergedCells(HSSFSheet sheet) {
    int numMerged = sheet.getNumMergedRegions();
    for (int i = 0; i < numMerged; i++) {
        CellRangeAddress mergedRegions = sheet.getMergedRegion(i);
        RegionUtil.setBorderLeft(BorderStyle.THIN, mergedRegions, sheet);
        RegionUtil.setBorderRight(BorderStyle.THIN, mergedRegions, sheet);
        RegionUtil.setBorderTop(BorderStyle.THIN, mergedRegions, sheet);
        RegionUtil.setBorderBottom(BorderStyle.THIN, mergedRegions, sheet);

    }
}

Do this for multiple rows.

Workbook wb = new HSSFWorkbook();

// create a new sheet
Sheet sheet = wb.createSheet();


CellStyle borderStyle = wb.createCellStyle();
borderStyle.setBorderBottom(CellStyle.BORDER_THIN);
borderStyle.setBorderLeft(CellStyle.BORDER_THIN);
borderStyle.setBorderRight(CellStyle.BORDER_THIN);
borderStyle.setBorderTop(CellStyle.BORDER_THIN);
borderStyle.setAlignment(CellStyle.ALIGN_CENTER);
Sheet sheet1 = wb.createSheet("Test Sheet");
Row row = null;
Cell cell;
for (int i = 1; i <= 5; ++i) {
    row = sheet1.createRow(i);
    for(int j=1;j<=5;j++){
        cell= row.createCell(j);
        cell.setCellStyle(borderStyle);
        if (i == 1 && j==1) {
            cell.setCellValue("Centred Text");
        } 
    }
}
sheet1.addMergedRegion(new CellRangeAddress(1, 5, 1, 5));

private void setBordersToMergedCells(XSSFWorkbook workBook, XSSFSheet sheet) {
        int numMerged = sheet.getNumMergedRegions();

    for(int i= 0; i<numMerged;i++){
        CellRangeAddress mergedRegions = sheet.getMergedRegion(i);
        RegionUtil.setBorderTop(CellStyle.BORDER_THIN, mergedRegions, sheet, workBook);
        RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, mergedRegions, sheet, workBook);
        RegionUtil.setBorderRight(CellStyle.BORDER_THIN, mergedRegions, sheet, workBook);
        RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, mergedRegions, sheet, workBook);
    }


}

In order to do this, you have to add a blank cell to every cell in the merged region, then add the appropriate borders to each cell. For example, the following code will create a merged region of 5 cells in the same row, with a border around the whole merged region, and the text centred in the region.

XSSFWorkbook wb = new XSSFWorkbook();
CellStyle borderStyle = wb.createCellStyle();
borderStyle.setBorderBottom(CellStyle.BORDER_THIN);
borderStyle.setBorderLeft(CellStyle.BORDER_THIN);
borderStyle.setBorderRight(CellStyle.BORDER_THIN);
borderStyle.setBorderTop(CellStyle.BORDER_THIN);
borderStyle.setAlignment(CellStyle.ALIGN_CENTER);
Sheet sheet = wb.createSheet("Test Sheet");
Row row = sheet.createRow(1);
for (int i = 1; i <= 5; ++i) {
    Cell cell = row.createCell(i);
    cell.setCellStyle(borderStyle);
    if (i == 1) {
        cell.setCellValue("Centred Text");
    } 
}
sheet.addMergedRegion(new CellRangeAddress(1, 1, 1, 5));