For column width, you could use SheetUtil.getColumnWidth: https://poi.apache.org/apidocs/org/apache/poi/ss/util/SheetUtil.html#getColumnWidth(org.apache.poi.ss.usermodel.Sheet,%20int,%20boolean) If you look at POI's source code, this function is called as part of Sheet.autoSizeColumn.
If you read through the implementation of the functions in SheetUtil [1], you'll see a .getX() and .getWidth(). You could copy the majority of getCellWidth to create getCellHeight, and replace the above with .getY() and .getHeight(). This would be the most general solution. As a hack, I suppose you could assume that all characters in the merged cell are the same font and the same size, then lookup the default character height for that font/size combination, and multiply and/or add by some constant to get the line height. (POI uses '0' as the default character, which doesn't extend below the line). If this is a template that is generated by POI so you know the font (say it's the default font/size), you could figure out an appropriate line height number by trial and error. If you do go the more general solution and develop something that others could use, please commit it back to the POI project. [1] http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/util/SheetUtil.java?view=markup On Tue, Apr 19, 2016 at 1:09 AM, Roberto Marchioni <[email protected]> wrote: > Hi Javen, > thank you for the answer. > How can I retrieve the best fit column width and the line spacing between > each line of text? > Which is the current column width, is it the sum of A and B? > > Thanks, > Roberto > > _________________________ > > Roberto Marchioni > Technical Dept. > > > > Tattile S.r.l. > Via Gaetano Donizetti, 1 - 25030 Mairano (BS) - Italy > ph.: +39 030 9700269 > [email protected] - www.tattile.com > > -----Messaggio originale----- > Da: Javen O'Neal [mailto:[email protected]] > Inviato: lunedì 18 aprile 2016 19.23 > A: POI Users List <[email protected]> > Oggetto: Re: Setting automatic height for merged cells > > Best fit column height = ceil(best fit column width / current column width) > * (height of a character + line spacing between each line of text) + margin > top + margin bottom > > This assumes all characters in the cell are the same font. > On Apr 18, 2016 9:20 AM, "Roberto Marchioni" <[email protected]> > wrote: > >> Hi everyone, >> I'm merging two cell (for example A2:B2) and I'm writing in the new region >> with the 'wrap' option enabled. >> I'm not able to automatically adjust the height of the row, so if the >> string wraps it's not correctly viewed. >> Is there a code to make it adjust it? >> >> Thanks everyone, >> Roberto >> >> _________________________ >> >> Roberto Marchioni >> Technical Dept. >> >> Tattile S.r.l. >> Via Gaetano Donizetti, 1 - 25030 Mairano (BS) - Italy >> ph.: +39 030 9700269 >> [email protected] - www.tattile.com >> > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
