80

Is there any other way to merge cells in Excel using Apache POI library?

I was trying using the following, but its not working

// selecting the region in Worksheet for merging data
CellRangeAddress region = CellRangeAddress.valueOf("A" + rowNo + ":D"
            + rowNo);

// merging the region
sheet1.addMergedRegion(region);
anderZubi
  • 6,414
  • 5
  • 37
  • 67
androidDev
  • 1,179
  • 4
  • 13
  • 31

5 Answers5

179

You can use sheet.addMergedRegion(rowFrom,rowTo,colFrom,colTo);

example sheet.addMergedRegion(new CellRangeAddress(1,1,1,4)); will merge from B2 to E2. Remember it is zero based indexing (ex. POI version 3.12).

for detail refer BusyDeveloper's Guide

Stefano
  • 3
  • 3
Sankumarsingh
  • 9,889
  • 11
  • 50
  • 74
  • 12
    if after this, Excel says it has to repair the file, make sure you're not overlapping cells when merging – golimar Mar 11 '16 at 17:35
  • @golimar What do you mean by overlapping cells? – James Kleeh Oct 05 '16 at 16:11
  • @JamesKleeh For example, creating one merged region A1+A2 and another merged region A2+A3 – golimar Oct 05 '16 at 21:14
  • 2
    Note that there are two versions of CellRangeAddress: the one to use is `org.apache.poi.ss.util.CellRangeAddress` whereas the other one `org.apache.poi.hssf.util.CellRangeAddress` is now deprecated. See https://stackoverflow.com/questions/22267679/what-to-use-instead-of-deprecated-cellrangeaddress-valueof-in-apachepoi – gordon613 Nov 17 '19 at 12:38
12

You can use :

sheet.addMergedRegion(new CellRangeAddress(startRowIndx, endRowIndx, startColIndx,endColIndx));

Make sure the CellRangeAddress does not coincide with other merged regions as that will throw an exception.

  • If you want to merge cells one above another, keep column indexes same
  • If you want to merge cells which are in a single row, keep the row indexes same
  • Indexes are zero based

For what you were trying to do this should work:

sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 3));
6

I create a method that merge cells and put border if you want.

protected void setMerge(Sheet sheet, int numRow, int untilRow, int numCol, int untilCol, boolean border) {
    CellRangeAddress cellMerge = new CellRangeAddress(numRow, untilRow, numCol, untilCol);
    sheet.addMergedRegion(cellMerge);
    if (border) {
        setBordersToMergedCells(sheet, cellMerge);
    }

}  

protected void setBordersToMergedCells(Sheet sheet, CellRangeAddress rangeAddress) {
    RegionUtil.setBorderTop(BorderStyle.MEDIUM, rangeAddress, sheet);
    RegionUtil.setBorderLeft(BorderStyle.MEDIUM, rangeAddress, sheet);
    RegionUtil.setBorderRight(BorderStyle.MEDIUM, rangeAddress, sheet);
    RegionUtil.setBorderBottom(BorderStyle.MEDIUM, rangeAddress, sheet);
}
Jesús Sánchez
  • 705
  • 11
  • 16
5

The best answer

sheet.addMergedRegion(new CellRangeAddress(start-col,end-col,start-cell,end-cell));
  • 4
    "The best answer" probably shouldn't include minus signs in variables. Also, the variables are outright wrong: columns are in the third and fourth position, not the first and second. – Reinderien Aug 11 '19 at 23:02
0

syntax is:

sheet.addMergedRegion(new CellRangeAddress(start-col,end-col,start-cell,end-cell));

Example:

sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 5));

Here the cell 0 to cell 5 will be merged of the 4th row.

Jayesh Babu
  • 1,389
  • 2
  • 20
  • 34