Adding border to a merged region in POI XSSF workbook

烂漫一生 提交于 2019-12-20 12:30:48

问题


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.


回答1:


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



回答2:


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


}



回答3:


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



回答4:


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

    }
}


来源:https://stackoverflow.com/questions/11277281/adding-border-to-a-merged-region-in-poi-xssf-workbook

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!