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);
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.
You can use sheet. addMergedRegion(rowFrom,rowTo,colFrom,colTo); example sheet. addMergedRegion(new CellRangeAddress(1,1,1,4)); will merge from B2 to E2.
setWrapText(true); cell. setCellStyle(cellStyle); Saving a file generated with the above code and viewing it in Microsoft Excel will show multiline text in a cell.
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
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.
For what you were trying to do this should work:
sheet.addMergedRegion(new CellRangeAddress(rowNo, rowNo, 0, 3));
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