Adding border to a merged region in POI XSSF workbook

I'm using apache poi 3.7 and I need to put border to a range of cells or merged region.

how can I to apply border to a merged region when the sheet and workbook type is XSSF. In HSSF type I use RegionUtil-/HSSFRegionutil, but if use the first object (Regionutil) in XSSF type its doesn't works and puts a black background color to the range of cells.

Regionutil ussually works with CellRangeAddress and i don't find information about this trouble. I don't know if the CellRangeAddres causes this.

Answers


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));

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);
    }


}

@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);

    }
}

Need Your Help

Django and Restful APIs

django api rest

I have been struggling with choosing a methodology for creating a RESTful API with Django. None of the approaches I've tried seem to be the "silver" bullet. WAPI from http://fi.am is probably the c...

colspan width issue

html css html-table

I'm having trouble setting fixed widths on columns which use colspan.