在POI生成的Excel文件中为单元格添加边框

时间:2011-04-19 17:08:38

标签: java excel border apache-poi

我正在使用POI生成Excel文件。我需要在工作表中的特定单元格中添加边框。

我该如何做到这一点?

8 个答案:

答案 0 :(得分:54)

以单元格中使用的样式设置边框将实现此目的。例如:

style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);

答案 1 :(得分:22)

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

答案 2 :(得分:13)

在较新的apache poi版本中:

XSSFCellStyle style = workbook.createCellStyle();
style.setBorderTop(BorderStyle.MEDIUM);
style.setBorderBottom(BorderStyle.MEDIUM);
style.setBorderLeft(BorderStyle.MEDIUM);
style.setBorderRight(BorderStyle.MEDIUM);

答案 3 :(得分:9)

辅助函数:

private void setRegionBorderWithMedium(CellRangeAddress region, Sheet sheet) {
        Workbook wb = sheet.getWorkbook();
        RegionUtil.setBorderBottom(CellStyle.BORDER_MEDIUM, region, sheet, wb);
        RegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM, region, sheet, wb);
        RegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM, region, sheet, wb);
        RegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM, region, sheet, wb);
    }

如果要在Excel中添加边框,则

String cellAddr="$A$11:$A$17";

setRegionBorderWithMedium(CellRangeAddress.valueOf(cellAddr1), sheet);

答案 4 :(得分:6)

XSSF

边框

使用XSSFCellStyle.BORDER_MEDIUMXSSFBorderFormatting.BORDER_MEDIUM(两个枚举引用相同的值):

XSSFCellStyle cellStyle = workbook.createCellStyle();

cellStyle.setBorderTop(XSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderRight(XSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderLeft(XSSFCellStyle.BORDER_MEDIUM);

cell.setCellStyle(cellStyle);

边框颜色

使用setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, XSSFColor)setBottomBorderColor(XSSFColor)(相当于上,左,右):

XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFColor color = new XSSFColor(new java.awt.Color(128, 0, 128));

cellStyle.setTopBorderColor(color);
cellStyle.setRightBorderColor(color);
cellStyle.setBottomBorderColor(color);
cellStyle.setLeftBorderColor(color);

cell.setCellStyle(cellStyle);

答案 5 :(得分:1)

RegionUtil上的4.0.0版开始-方法具有新的签名。例如:

RegionUtil.setBorderBottom(BorderStyle.DOUBLE,
            CellRangeAddress.valueOf("A1:B7"), sheet);

答案 6 :(得分:1)

如果您使用的是 org.apache.poi.ss.usermodel (不是HSSF或XSSF),则可以使用:

style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);

apache文档中的所有边框样式均为here

答案 7 :(得分:0)

要在Apache POI中创建边框,您应该......

1:创建样式

final XSSFCellStyle style = workbook.createCellStyle();

2:然后你必须创建边框

style.setBorderBottom( new XSSFColor(new Color(235,235,235));

3:然后你必须设置那个边框的颜色

style.setBottomBorderColor( new XSSFColor(new Color(235,235,235));

4:然后将样式应用于单元格

cell.setCellStyle(style);