I'm using Apache POI and I need to put a border in a range of cells or merged region. I am merging the cells with three rows and five columns. But I am not able to add the border to it. So how do I do this?
Go to Format Cells-->Alignment-->Horizontal and drop the Horizontal box down and select "Center Across Selection". You can then use the range of cells to use BorderAround.
sheet = // existing Sheet setup int firstRow = 0; int lastRow = 0; int firstCol = 0; int lastCol = 2 sheet. addMergedRegion(new CellRangeAddress(firstRow, lastRow, firstCol, lastCol)); We can also use a cell range reference string to provide the merged region: sheet = // existing Sheet setup sheet.
My solution was to merge the cells by their positions, then created a cell (reference to the first block of the merged cells) to assign a value and then set the border throught the HSSFRegionUtil
// Merges the cells
CellRangeAddress cellRangeAddress = new CellRangeAddress(start, start, j, j + 1);
sheet.addMergedRegion(cellRangeAddress);
// Creates the cell
Cell cell = CellUtil.createCell(row, j, entry.getKey());
// Sets the borders to the merged cell
HSSFRegionUtil.setBorderTop(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderLeft(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderRight(CellStyle.BORDER_MEDIUM, cellRangeAddress, sheet, workbook);
HSSFRegionUtil.setBorderBottom(CellStyle.BORDER_THIN, cellRangeAddress, sheet, workbook);
You will find the RegionUtil class useful for settings the borders for a range of cells.Look here:
http://poi.apache.org/apidocs/index.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With