Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向POI生成的Excel文件中的单元格添加边框_Java_Excel_Border_Apache Poi - Fatal编程技术网

Java 向POI生成的Excel文件中的单元格添加边框

Java 向POI生成的Excel文件中的单元格添加边框,java,excel,border,apache-poi,Java,Excel,Border,Apache Poi,我正在使用POI生成Excel文件。我需要为工作表中的特定单元格添加边框 如何实现这一点?以单元格中使用的样式设置边框将实现这一点。例如: style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM); style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM); style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM); style.setBorderLeft(HSSFCell

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


如何实现这一点?

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

style.setBorderBottom(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderRight(HSSFCellStyle.BORDER_MEDIUM);
style.setBorderLeft(HSSFCellStyle.BORDER_MEDIUM);
XSSF 边框类型 使用
XSSFCellStyle.BORDER_MEDIUM
XSSFBorderFormatting.BORDER_MEDIUM
(两个枚举引用相同的值):

边框颜色 使用
setBottomBorderColor(XSSFCellBorder.BorderSide.BOTTOM,XSSFColor)
setBottomBorderColor(XSSFColor)
(等同于顶部、左侧、右侧):

辅助函数:

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),第页)

在较新的apache poi版本中:

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

要在ApachePOI中创建边框,您应该

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);

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

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

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

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

所有的边框样式都在apache文档中

这对我来说很有效,但我仍然愿意接受更好的解决方案。特别是那些不涉及新样式只是为了更改边框的样式。样式是最常用的方式,Excel通常会使用样式或图形来完成这类工作。@Gagravarr,谢谢。我想对于同一个文本必须有两种样式,但一种有边框,另一种没有边框,这似乎是一种痛苦。特别是如果你想在外边缘有一个更坚固的边界。我希望有更好的方法。我唯一能建议的就是试着用Excel来做,看看Excel本身在文件中做了什么。我很怀疑,你会发现Excel会创造出一些风格来做这件事……干得好@winklerr终于有人读到了问题!它是XSSF而不是HSSF
cell.setCellStyle(style);
RegionUtil.setBorderBottom(BorderStyle.DOUBLE,
            CellRangeAddress.valueOf("A1:B7"), sheet);
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);