Reputation: 23639
I am using POI to generate an Excel File. I need to add borders to specific cells in the worksheet.
How can I accomplish this?
Upvotes: 53
Views: 148438
Reputation: 14867
Use XSSFCellStyle.BORDER_MEDIUM
or XSSFBorderFormatting.BORDER_MEDIUM
(both enums refer to the same value):
final 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);
Use setBorderColor(XSSFCellBorder.BorderSide.BOTTOM, XSSFColor)
or setBottomBorderColor(XSSFColor)
(equivalent for top, left, right):
final XSSFCellStyle cellStyle = workbook.createCellStyle();
final 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);
Upvotes: 15
Reputation: 397
If you're using the org.apache.poi.ss.usermodel (not HSSF or XSSF) you can use:
style.setBorderBottom(BorderStyle.THIN);
style.setBorderTop(BorderStyle.THIN);
style.setBorderLeft(BorderStyle.THIN);
style.setBorderRight(BorderStyle.THIN);
all the border styles are here at the apache documentation
Upvotes: 5
Reputation: 921
From Version 4.0.0 on RegionUtil
-methods have a new signature. For example:
RegionUtil.setBorderBottom(BorderStyle.DOUBLE,
CellRangeAddress.valueOf("A1:B7"), sheet);
Upvotes: 3
Reputation: 939
To create a border in Apache POI you should...
1: Create a style
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
style.setBottomBorderColor( new XSSFColor(new Color(235,235,235));
4: Then apply the style to a cell
cell.setCellStyle(style);
Upvotes: 1
Reputation: 5095
In the newer apache poi versions:
XSSFCellStyle style = workbook.createCellStyle();
style.setBorderTop(BorderStyle.MEDIUM);
style.setBorderBottom(BorderStyle.MEDIUM);
style.setBorderLeft(BorderStyle.MEDIUM);
style.setBorderRight(BorderStyle.MEDIUM);
Upvotes: 39
Reputation: 1190
a Helper function:
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
String cellAddr="$A$11:$A$17";
setRegionBorderWithMedium(CellRangeAddress.valueOf(cellAddr1), sheet);
Upvotes: 15
Reputation: 1210
HSSFCellStyle style=workbook.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBorderTop(HSSFCellStyle.BORDER_THIN);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
Upvotes: 25
Reputation: 23639
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);
Upvotes: 68