java 使用 HSSFRegionUtil (Apache POI) 向合并单元格添加边框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15129385/
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-10-31 18:35:00  来源:igfitidea点击:

Adding border to merged cell using HSSFRegionUtil (Apache POI)

javaexcelapache-poi

提问by zawhtut

I'm using Apache POI and found that when I add border to the merged cells using HSSFRegionUtil , border for only one cell is appeared.

我正在使用 Apache POI,发现当我使用 HSSFRegionUtil 向合并的单元格添加边框时,只会出现一个单元格的边框。

Following is the code

以下是代码

    Region region = new Region((short)0,(short)0,(short)1,(short)0);
    sheet.addMergedRegion(region);
    HSSFRegionUtil.setBorderRight(HSSFCellStyle.BORDER_MEDIUM,region, sheet, workbook);

It only showing cell border for just one cell and not the region border. Any kind help is appreciated.

它只显示一个单元格的单元格边框,而不是区域边框。任何帮助表示赞赏。

回答by Vargan

Personally, I don't use HSSFRegionUtil for add a border to a merged region. My solution is:

就个人而言,我不使用 HSSFRegionUtil 为合并区域添加边框。我的解决办法是:

assuming you're merge the region from row 1, column 1, until row 5, column 5

假设您要合并从第 1 行第 1 列到第 5 行第 5 列的区域

1) set the cellStyle to the cell 1,1

1) 将 cellStyle 设置为单元格 1,1

2) the write a method that copy the cell style to all the others cells.

2)编写一个将单元格样式复制到所有其他单元格的方法。

3) merge the region

3)合并区域

here an example:

这里有一个例子:

    CellStyle style = defaultBorderStyle;
    Row tempRow = currentSheet.getRow(startRow);
    Cell cell = tempRow.getCell(startColumn);

    for (int i = startRow; i <= endRow; i++) {
        tempRow = currentSheet.getRow(i);

        for (int j = startColumn; j <= endColumn  ; j++) {
            tempRow.getCell(j).setCellStyle(style);
        }

    }
    sheet.addMergedRegion(new CellRangeAddress(startRow, endRow, startColumn, endColumn));