Java 为 POI 中的合并单元格创建边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23533381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:39:47  来源:igfitidea点击:

Creating the border for the merged cells in POI

javaapache-poi

提问by user3511026

Could anyone explain me how to create the borders for the merged cells using Apache POI?
The Code I'm using is only affecting one cell.

谁能解释我如何使用 Apache POI 为合并的单元格创建边界?
我使用的代码只影响一个单元格。

sheet.addMergedRegion(new CellRangeAddress(1, 1, 2, 3));
Cell monthCell = subheaderRow.createCell(2);
monthCell.setCellValue(2);
monthCell.setCellStyle(styles.get("month"));



style = wb.createCellStyle();

style.setBorderBottom(CellStyle.BORDER_THIN);
style.setBorderTop(CellStyle.BORDER_THIN);
style.setBorderLeft(CellStyle.BORDER_THIN);
style.setBorderRight(CellStyle.BORDER_THIN);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setAlignment(CellStyle.ALIGN_CENTER);
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER);
// style.setFillForegroundColor(IndexedColors.DARK_BLUE.getIndex());
// style.setFillPattern(CellStyle.SOLID_FOREGROUND);
// style.setFont(monthFont);
styles.put("month", style);

回答by Alan Hay

This can be done as below:

这可以如下完成:

public void doMerge(int rowIndex, int columnIndex, int rowSpan, int columnSpan) {
    Cell cell = sheet.getRow(rowIndex).getCell(columnIndex);
    CellRangeAddress range = new CellRangeAddress(rowIndex, rowIndex + rowSpan - 1, columnIndex, columnIndex
            + columnSpan - 1);

    sheet.addMergedRegion(range);

    RegionUtil.setBorderBottom(cell.getCellStyle().getBorderBottom(), range, sheet, sheet.getWorkbook());
    RegionUtil.setBorderTop(cell.getCellStyle().getBorderTop(), range, sheet, sheet.getWorkbook());
    RegionUtil.setBorderLeft(cell.getCellStyle().getBorderLeft(), range, sheet, sheet.getWorkbook());
    RegionUtil.setBorderRight(cell.getCellStyle().getBorderRight(), range, sheet, sheet.getWorkbook());

    RegionUtil.setBottomBorderColor(cell.getCellStyle().getBottomBorderColor(), range, sheet, sheet.getWorkbook());
    RegionUtil.setTopBorderColor(cell.getCellStyle().getTopBorderColor(), range, sheet, sheet.getWorkbook());
    RegionUtil.setLeftBorderColor(cell.getCellStyle().getLeftBorderColor(), range, sheet, sheet.getWorkbook());
    RegionUtil.setRightBorderColor(cell.getCellStyle().getRightBorderColor(), range, sheet, sheet.getWorkbook());
}

回答by Sam

If you want to add border to all the merged cells, first you got to create a dummy cells for all the rows and columns that are merged(not only for the cells you are using, but for ALL). Then apply the style. Say you want to merge cells from Column 1 to 10 and Rows 0 and 1. Create dummy cells for those rows and columns, and override this to create your cells.

如果要为所有合并的单元格添加边框,首先必须为合并的所有行和列创建一个虚拟单元格(不仅为您正在使用的单元格,而且为所有单元格)。然后应用样式。假设您要合并从第 1 列到第 10 行以及第 0 行和第 1 行的单元格。为这些行和列创建虚拟单元格,并覆盖它以创建您的单元格。

Loop

环形

Row myRow1= sheet.createRow((short) 0);
Row myRow2 = sheet.createRow((short) 1);
for (int i = 1; i <= 10; ++i) 
     {
            Cell blankCell1 = myRow1.createCell(i);
            blankCell1.setCellStyle(style);
            Cell blankCell2 = myRow2.createCell(i);
            blankCell2.setCellStyle(style);
     } 

For Style

风格

HSSFCellStyle style= wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);

Creating WorkBook

创建工作簿

HSSFWorkbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet("Name of the Excel");

For Merging

用于合并

sheet.addMergedRegion(new CellRangeAddress(0, 1, 1, 10)); //(fromRow, toRow, fromColumn,toColumn)

Hope this helps.

希望这可以帮助。