Glad you posted that as I was just logging on to say that 'my' solution will
not work and indeed I made a mistake in the Excel behaviour I reported.

It seems that as soon as cells are merged then the auto fit behaviour simply
disappears
(http://excel.tips.net/Pages/T003207_Automatic_Row_Height_For_Merged_Cells_with_Text_Wrap.html).
There are tricks you can use but they all do entail calculating and setting
the row height(s) manually. Sorry to mislead anyone.

Yours

Mark B


TimShiu wrote:
> 
> Thanks for everyone's help on this topic.
> 
> I finally got another solution and would like to share it here.
> My new solution is to calculate how many lines the text will be needed by
> using LineBreakMeasurer and set the height of the row accordingly.
> 
> // =======================
> // Program segment [START]
> // =======================
> 
> // Create Font object with Font attribute (e.g. Font family, Font size,
> etc) for calculation
> java.awt.Font currFont = new java.awt.Font(fontName, 0, fontSize);
> AttributedString attrStr = new AttributedString(cellValue);
> attrStr.addAttribute(TextAttribute.FONT, currFont);
> 
> // Use LineBreakMeasurer to count number of lines needed for the text
> FontRenderContext frc = new FontRenderContext(null, true, true);
> LineBreakMeasurer measurer = new LineBreakMeasurer(attrStr.getIterator(),
> frc);
> int nextPos = 0;
> int lineCnt = 0;
> while (measurer.getPosition() < cellValue.length())
> {
>     nextPos = measurer.nextOffset(mergedCellWidth); // mergedCellWidth is
> the max width of each line
>     lineCnt++;
>     measurer.setPosition(nextPos);
> }
> 
> Row currRow = currSht.getRow(rowNum);
> currRow.setHeight((short)(currRow.getHeight() * lineCnt));
> 
> // =====================
> // Program segment [END]
> // =====================
> 
> The above solution didn't handle the newline character, i.e. "\n", and
> only tested under horizontal merged cells only.
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Autosize-row-for-HSSF-library-tp24132911p24218279.html
Sent from the POI - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to