JAVA Apache POI Excel:为单元格区域添加边框

时间:2015-02-17 15:28:07

标签: java excel apache apache-poi

有没有办法使用带有Apache POI的Java为cellrange添加边框?

像A1:B2应该得到一个左上角 - 右下角的粗边框?

我知道如何制作&将样式应用于单个单元格,我可以通过单元格迭代并应用适当的样式,但我确信这样做更容易。

3 个答案:

答案 0 :(得分:11)

我已经弄清楚了。在apache poi页面上实际上有一个示例,我找不到我一直在搜索的关键字。

CellRangeAddress region = CellRangeAddress.valueOf(A1:B2);
short borderStyle = CellStyle.BORDER_MEDIUM;
RegionUtil.setBorderBottom(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderTop(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderLeft(borderStyle, region, activeSheet, excelWorkbook);
RegionUtil.setBorderRight(borderStyle, region, activeSheet, excelWorkbook);

答案 1 :(得分:3)

3.16中的情况发生了变化

CellRangeAddress region = new CellRangeAddress(6, 8, 1, 10);
RegionUtil.setBorderBottom(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderTop(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderLeft(BorderStyle.THIN, region, sheet);
RegionUtil.setBorderRight(BorderStyle.THIN, region, sheet);

答案 2 :(得分:0)

在我的情况下,

RegionUtil 将除了白色以外的所有背景颜色都变为黑色。 这是我的解决方法:

public final class BorderUtils {

    public static void setBorder(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
        setBorderTop(sheet, borderStyle, region);
        setBorderBottom(sheet, borderStyle, region);
        setBorderLeft(sheet, borderStyle, region);
        setBorderRight(sheet, borderStyle, region);
    }

    public static void setBorderTop(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
        Row row = sheet.getRow(region.getFirstRow());
        for (int i=region.getFirstColumn() ; i<=region.getLastColumn() ; i++) {
            Cell cell = row.getCell(i);
            CellStyle cellStyle = cloneCellStyle(sheet, cell);
            cellStyle.setBorderTop(borderStyle);
            cell.setCellStyle(cellStyle);
        }
    }

    public static void setBorderBottom(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
        Row row = sheet.getRow(region.getLastRow());
        for (int i=region.getFirstColumn() ; i<=region.getLastColumn() ; i++) {
            Cell cell = row.getCell(i);
            CellStyle cellStyle = cloneCellStyle(sheet, cell);
            cellStyle.setBorderBottom(borderStyle);
            cell.setCellStyle(cellStyle);
        }
    }

    public static void setBorderLeft(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
        for (int i=region.getFirstRow() ; i<=region.getLastRow() ; i++) {
            Cell cell = sheet.getRow(i).getCell(region.getFirstColumn());
            CellStyle cellStyle = cloneCellStyle(sheet, cell);
            cellStyle.setBorderLeft(borderStyle);
            cell.setCellStyle(cellStyle);
        }
    }

    public static void setBorderRight(Sheet sheet, BorderStyle borderStyle, CellRangeAddress region) {
        for (int i=region.getFirstRow() ; i<=region.getLastRow() ; i++) {
            Cell cell = sheet.getRow(i).getCell(region.getLastColumn());
            CellStyle cellStyle = cloneCellStyle(sheet, cell);
            cellStyle.setBorderRight(borderStyle);
            cell.setCellStyle(cellStyle);
        }
    }

}