I want to form a excel output as below in POI:
As clear from the image
in reality I did not understand your problem completely, but I have some key points, that I think, will be helpful to you.
The first Cell of merged region keeps the value of the merged cell, rest cells keep blank value. Means if cell A2 to A5 is merged with value "Test" then on iteration A2 will show "Test" while rest will show blank. SO if you want to read the merged cell value, you need to read its first cell only, and similarly if you want to write in the merged cells, you just need to write in first cell only.
sheet.getNumMergedRegions();
return integer that will be the total number of merged region in the sheet. you can iterate through it using loop as well.
CellRangeAddress merge = sheet.getMergedRegion(int index);
will give you the range address of the specified index obtained from getNumMergedRegions()
now merge.getFirstRow()
,merge.getLastRow()
, merge.getFirstColumn()
, merge.getLastColumn()
, merge.getNumberOfCells()
, are the methods for the same.
The iteration through row containing merged cells and row not containing any merged cell are not much different. Hope this will help you to resolve your problem.