添加边框合并单元格在Excel中的Apache POI的Java?(Add border to me

2019-07-05 15:45发布

我使用Apache POI,我需要把边框的单元格区域或合并区域。 我合并的细胞三行五列。 但我不能够将边框添加到它。 所以,我该怎么办呢?

Answer 1:

我的解决办法是通过它们的位置合并的单元格,则创建了一个小区(参考合并单元的第一个块)分配一个值,然后设置边框throught的HSSFRegionUtil

// Merges the cells
CellRangeAddress cellRangeAddress = new CellRangeAddress(start, start, j, j + 1);
sheet.addMergedRegion(cellRangeAddress);

// Creates the cell
Cell cell = CellUtil.createCell(row, j, entry.getKey());

// Sets the borders to the merged cell
HSSFRegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderBottom(CellStyle.BORDER_THIN, cellRangeAddress, sheet, workbook);


Answer 2:

你会发现RegionUtil类设置边界的范围cells.Look这里有用:

http://poi.apache.org/apidocs/index.html



Answer 3:

首先,它会很高兴地知道哪些表格式,您要创建。 因为当你在HSSF合并空细胞,这是完全正常的,而XSSF创建格式错误的文件造成的错误,同时在Microsoft Excel中打开它。 风格往往在两种情况下,同样的行为。 您需要分配相同的风格(在包括边框的情况下,风格)对要合并的每个细胞。 我的建议创建一个检查和纠正,设置风格在合并区域中的所有细胞的功能。 这里是比如我自己的:

private static final XSSFColor COLOR_ORANGE         = new XSSFColor(new java.awt.Color(254, 253, 189));
private static final XSSFColor COLOR_GREY           = new XSSFColor(new java.awt.Color(191, 190, 154));

。 。 。

        XSSFCellStyle styleSubHeader = (XSSFCellStyle) wb.createCellStyle();
        styleSubHeader.setFont(fontBold);
        styleSubHeader.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
        styleSubHeader.setAlignment(CellStyle.ALIGN_CENTER);
        styleSubHeader.setFillForegroundColor(COLOR_ORANGE);
        styleSubHeader.setFillPattern(CellStyle.SOLID_FOREGROUND);
        styleSubHeader.setBorderBottom(CellStyle.BORDER_THIN);
        styleSubHeader.setBottomBorderColor(COLOR_GREY);
        styleSubHeader.setBorderLeft(CellStyle.BORDER_THIN);
        styleSubHeader.setLeftBorderColor(COLOR_GREY);
        styleSubHeader.setBorderRight(CellStyle.BORDER_THIN);
        styleSubHeader.setRightBorderColor(COLOR_GREY);
        styleSubHeader.setBorderTop(CellStyle.BORDER_THIN);
        styleSubHeader.setTopBorderColor(COLOR_GREY);

。 。 。

 /**
 * Checking if every row and cell in merging region exists, and create those which are not    
 * @param sheet in which check is performed
 * @param region to check
 * @param cellStyle cell style to apply for whole region
 */
private void cleanBeforeMergeOnValidCells(XSSFSheet sheet,CellRangeAddress region, XSSFCellStyle cellStyle )
{
    for(int rowNum =region.getFirstRow();rowNum<=region.getLastRow();rowNum++){
        XSSFRow row= sheet.getRow(rowNum);
        if(row==null){
            sheet.createRow(rowNum);
            logger.trace("while check row "+rowNum+" was created");
        }
        for(int colNum=region.getFirstColumn();colNum<=region.getLastColumn();colNum++){
            XSSFCell currentCell = row.getCell(colNum); 
           if(currentCell==null){
               currentCell = row.createCell(colNum);
               logger.trace("while check cell "+rowNum+":"+colNum+" was created");
           }    

           currentCell.setCellStyle(cellStyle);

        }
    }


}

最后,你实际的合并通话这样的事情之前调用它:

CellRangeAddress region = new CellRangeAddress(rowIndex, rowIndex, mergeStart, cellIndex+cellOffset);
cleanBeforeMergeOnValidCells(row.getSheet(),region,styleSubHeader );
row.getSheet().addMergedRegion(region);// merging cells that has a title name

希望能帮助到你。



Answer 4:

我建议你使用getMergedRegions,该方法将在表返回合并的区域列表。 然后,您可以遍历每个区域应用边框。 例如:

private void setBordersToMergedCells(Workbook workBook, Sheet sheet) {
  List<CellRangeAddress> mergedRegions = sheet.getMergedRegions();
  for (CellRangeAddress rangeAddress : mergedRegions) {
    RegionUtil.setBorderTop(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
    RegionUtil.setBorderLeft(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
    RegionUtil.setBorderRight(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
    RegionUtil.setBorderBottom(CellStyle.BORDER_THIN, rangeAddress, sheet, workBook);
  }
}

然后,你可以当你做了所有的合并,你在MySheet的工作需要调用此方法。



Answer 5:

使用:

int rownumm=0;
rownumm++;

Row row = sheet.createRow(rownumm);
Cell cell = row.createCell(0);
cell.setCellValue(web.getUrl());
cell.setCellStyle(styles.get("font"));//font for text

CellRangeAddress region = CellRangeAddress.valueOf("$A$"+ (rownumm) + ":$E$+" + (rownumm));
frame(region, sheet, wb); 

而且使用方法:

private static void frame(CellRangeAddress region,Sheet sheet, Workbook wb){
    sheet.addMergedRegion(region);

    final short borderMediumDashed = CellStyle.BORDER_MEDIUM;
    RegionUtil.setBorderBottom(borderMediumDashed, region, sheet, wb);
    RegionUtil.setBorderTop(borderMediumDashed, region, sheet, wb);
    RegionUtil.setBorderLeft(borderMediumDashed, region, sheet, wb);
    RegionUtil.setBorderRight(borderMediumDashed, region, sheet, wb);
}

另请参见链接“使用便利功能”:

https://poi.apache.org/spreadsheet/quick-guide.html#FooterPageNumbers



Answer 6:

    cellStyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
    cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
    cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
    cellStyle.setBottomBorderColor(IndexedColors.BLACK.getIndex());
    cellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
    cellStyle.setLeftBorderColor(IndexedColors.BLACK.getIndex());
    cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());


文章来源: Add border to merged cells in excel Apache poi java.?