I am creating an big excel using java class. Excel contains a merged cell which store string. Length of the string is very large, i am getting this string dynamically. I need to increase the height of the merged cell so that complete string will fit to that cell. I have tried using "wrap text", it wrap the text but doesn't increase the height of the merged cell due to which complete string is not visible in excel. Java class i am using are:
- XSSFWorkbook
- XSSFSheet
- XSSFRow
- XSSFCell
- XSSFCellStype
And other required dependent classes. Is there a way how i can increase the height of the merge cell as per the cell value.
To give you an idea of the challenge how to calculate the needed row height.
We can get the column width in character widths for the columns using Sheet.getColumnWidth(int). But this is not really accurate since it only is for number glyphs: 1,2,3,4,5,6,7,8,9,0. In true type fonts there are glyphs (., |, l, ...) which need much less width. So this result will be as much more inaccurate as such less-width-glyphs are used in the text.
We might correct the column-width-in-chars by a factor. I use 5/4. But this is highly dependent on the language used.
Then we can calculate the needed row count and then get the needed row height by getting the default row height used for one line and multiply that with the needed row count .
This works because default font and font size is used. The challenge increases up to infinity when different fonts and different font sizes are used ;-).
See How to get the needed height of a multi line rich-text field (any font, any font size) having defined width using Java? for an example rendering the formatted text in a
JTextPane
to get the preferred height.