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.


MSB wrote:
> 
> I know that this will not help anyone but the behviour you describe is
> just what Excel itself does.
> 
> This morning, using Excel, I opened a new worksheet, increased the width
> of column A, clicked on cell A1 and set wrap text to true. When I typed a
> long string ot text into that cell, it wrapped around and the row did
> resize itself automatically.
> 
> Next, I clicked on cell A4, set Wrap Text to true and then merged cells A4
> and A5. Typing a lone string into that merged cell I saw it wrap but the
> row(s) did not autore-size.
> 
> However, I did find one way to make Excel do what you are after. The trick
> was to set the wrap text attribute of both cells in the merged region to
> true. I will not have the time to test this in POI for a few hours so to
> explain what I did in Excel;
> 
> Click on cell A6 and set the wrap text attribute true. Click on cell A7
> and set the wrap text attribute to true. Merge cells A6 and A7. Type a
> very long string of text into cell A6. To re-iterate, it seems that the
> key is to set the wrap text attribute on the cells in the merged region; I
> do not know yet how multi column merged regions will work, that is another
> test.
> 
> Hop that helps and I willpost some POI code later if I have the chance.
> 
> Yours
> 
> Mark B
> 
> 
> TimShiu wrote:
>> 
>> In excel, the row height will be auto resized if any cell in the row is
>> set as wrap-text.
>> Unfortunately, this auto resizing behavior will be malfunctioned when the
>> cell is a merged cell.
>> 
>> So, I would like to ask if there is any method in HSSF library that can
>> autosize the row????
>> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Autosize-row-for-HSSF-library-tp24132911p24216153.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