This project is read-only.

Auto Size Row

Jul 21, 2010 at 3:22 PM
Hi, I can't find any AutoSizeRow method (that would be similar to the existing AutoSizeColumn, but for the row height). Is it supported by the current version of NPOI ? If not, is there any known workarounds ? Thanks!
Jul 21, 2010 at 6:30 PM
There is no AutoSizeRow method implemented in NPOI, the only height settings you can make are by using one of the properties HSSFRow.Height or HSSFRow.HeightInPoints. What are you trying to work around?
Jul 21, 2010 at 7:25 PM
Our program generated rows can have multiple lines (they can contain multiple newline characters). An AutoSizeRow would make sure that no cell content is hidden. We can probably try to work around this by counting the number of lines and setting the height accordingly. I just wanted to make sure that we were not going to implement something that is already supported by the library. Thanks for the feedback !
Jul 22, 2010 at 2:34 PM

As a workaround, could you not set the cell style and have wraptext = true?

The following worked for me (except when I had merged cells)

HSSFRow row = sheet.CreateRow(0);
HSSFCell cell = row.CreateCell(0);
cell.SetCellValue("Some text\n and with \n multiple line breaks in \n it");

//Create a cell style
HSSFCellStyle style = hssfworkbook.CreateCellStyle();

//wrap the text
style.WrapText = true;

//assign the style to the cell
cell.CellStyle = style;

//don't know why this works, but if you add in a new row 
//and set it's height, the previous row(s) will autosize (nb only
//tested viewing in Excel 2007)

row = sheet.CreateRow(1);
row.HeightInPoints = 15;


Jul 22, 2010 at 6:27 PM
Thanks for the tip. The combination of WrapText + CreateRow + HeightInPoints on the extra row, does work on Excel. Unfortunately, it doesn't work with OpenOffice 3.1.