Merging cells in Excel using Apache POI
Try this following solution :
Use this sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo);
For example sheet.addMergedRegion(new CellRangeAddress(1,1,1,4)); will be joining from B2 to E2. Extract it is based on based indexing, example. POI version 3.12.
For more information, use it BusyDeveloper’s Guide
Try this explanation :
sheet.addMergedRegion(new CellRangeAddress(startRowIndx, endRowIndx, startColIndx,endColIndx));
Note the Cell Range Address does not go along with other mingle regions as that will throw an exception.
- Maintain column indexes same when need to mingle cells one above another.
- Maintain the row indexes same when need to mingle cells that are in a single row.
- Indexes are based on zero.
Use this code :
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 3));