Merge cells in a row or merge cells in column -
Create a workbook and sheet in the workbook-
XSSFWorkbook workBook = new XSSFWorkbook();Sheet sheet = workBook.createSheet("My sheet");sheet.addMergedRegion(new CellRangeAddress(startRowIndx,
endRowIndx,
startColIndx,
endColIndx));
try (FileOutputStream outputStream = new FileOutputStream( "demo.xlsx" )){
workbook.write( outputStream );}
finally
{
workbook.close();
} 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, it means row and column start from zero.
- Start index of row and column must be less the end index of row and column.

Comments
Post a Comment