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

时间:2021-03-13 20:23:10

I am using POI to generate an Excel File. I need to add borders to specific cells in the worksheet.

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

How can I accomplish this?

我怎样才能做到这一点呢?

6 个解决方案

#1


47  

Setting up borders in the style used in the cells will accomplish this. Example:

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

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

#2


18  

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

#3


6  

a Helper function:

一个Helper函数:

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

When you want to add Border in Excel, then

当您想在Excel中添加边框时

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

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

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

#4


4  

XSSF

BorderStyle

Use XSSFCellStyle or XSSFBorderFormatting (both enums refer to the same value).

使用XSSFCellStyle或XSSFBorderFormatting(两个枚举引用相同的值)。

XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(XSSFBorderFormatting.BORDER_MEDIUM);

BorderColor

Use setBorderColor(BorderSide, XSSFColor) or setBottomBorderColor(XSSFColor) (equivalent for top, left, right).

使用setBorderColor(border, XSSFColor)或setBottomBorderColor(XSSFColor)(等效于top, left, right)。

XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFColor color = new XSSFColor(new java.awt.Color(128, 0, 128));
cellStyle.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, color);
cellStyle.setBottomBorderColor(color);

#5


4  

In the newer apache poi versions:

在更新的apache poi版本中:

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

#6


1  

To create a border in Apache POI you should...

要在Apache POI中创建边界,您应该……

1: Create a style

1:创建一个样式

final XSSFCellStyle style = workbook.createCellStyle();

2: Then you have to create the border

他说:那你必须创造边界

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


3: Then you have to set the color of that border


3:那么你必须设置的颜色边界

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

4: Then apply the style to a cell

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

cell.setCellStyle(style);

#1


47  

Setting up borders in the style used in the cells will accomplish this. Example:

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

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

#2


18  

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

#3


6  

a Helper function:

一个Helper函数:

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

When you want to add Border in Excel, then

当您想在Excel中添加边框时

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

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

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

#4


4  

XSSF

BorderStyle

Use XSSFCellStyle or XSSFBorderFormatting (both enums refer to the same value).

使用XSSFCellStyle或XSSFBorderFormatting(两个枚举引用相同的值)。

XSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setBorderBottom(XSSFCellStyle.BORDER_MEDIUM);
cellStyle.setBorderTop(XSSFBorderFormatting.BORDER_MEDIUM);

BorderColor

Use setBorderColor(BorderSide, XSSFColor) or setBottomBorderColor(XSSFColor) (equivalent for top, left, right).

使用setBorderColor(border, XSSFColor)或setBottomBorderColor(XSSFColor)(等效于top, left, right)。

XSSFCellStyle cellStyle = workbook.createCellStyle();
XSSFColor color = new XSSFColor(new java.awt.Color(128, 0, 128));
cellStyle.setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, color);
cellStyle.setBottomBorderColor(color);

#5


4  

In the newer apache poi versions:

在更新的apache poi版本中:

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

#6


1  

To create a border in Apache POI you should...

要在Apache POI中创建边界,您应该……

1: Create a style

1:创建一个样式

final XSSFCellStyle style = workbook.createCellStyle();

2: Then you have to create the border

他说:那你必须创造边界

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


3: Then you have to set the color of that border


3:那么你必须设置的颜色边界

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

4: Then apply the style to a cell

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

cell.setCellStyle(style);