Adding border to a merged region in POI XSSF workbook

后端 未结 4 961
醉话见心
醉话见心 2021-02-05 21:11

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

4条回答
  •  有刺的猬
    2021-02-05 21:44

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

提交回复
热议问题