We have dynamic content to be displayed in excel(xlsx) file. First couple of rows will have table data after table data we need to show text(in merged region). There will not be any limit to the character count.

We used below snippet to create merged cell region
Lets say table rows are 10 and columns are 5 (these are not constants)
XSSFSheet xssfSheet; //valid xssfSheet
int firstRow; //row after table data. table rows + 2, so firstRow will be 12
int lastRow; // firstRow+<...> (we have some custom logic to identify this)
int firstCol; //always 0
int lastCol; // table column size lastCol will be 5, (table columns )
CellRangeAddress region = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);

From above snippet lastRow is being calculated using total number of characters and some other stuff and then determining the lastRow value to show content fit to merged region. (sometimes content fits to merged region, sometimes content overflows the region and sometimes content is less and white space appears). I am sure this is not elegant way to do it. I couldn't find any other way.

I need to understand what would be the right way to fit the content in merged region without content overflow or too much white space

Any help would be appreciative