Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JAVA Apache POI Excel: add borders to cell range

Is there a way to add borders to a cellrange using Java with Apache POI?

Like A1:B2 should get a top-bottom-left-right thick border - style?

I know how to create & apply styles to single cells and I might iterate trough the cells and apply the appropriate styles but I'm sure there's an easier way.

like image 476
Joel Avatar asked Feb 17 '15 15:02

Joel


2 Answers

I've been able to figure it out. There is actually a sample on the apache poi page I just didn't find with the keywords I've been searching with.

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);
like image 122
Joel Avatar answered Sep 28 '22 00:09

Joel


Things have changed in 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);
like image 27
Shawn Avatar answered Sep 28 '22 02:09

Shawn