0

I'm trying to produce an Excel workbook with one worksheet where the content in the cells wraps AND the row height is autofit when the columns are resized. The word wrapping seems to work OK, but I can't get the row auto height to work. I have looked at several SO posts and many of the different methods available in the POI API, but none of them seem to work.

The post at Auto size height for rows in Apache POI does not seem to work. Using methods like

row.setStyle(style)
row.setHeight((short)-1) 

do not work either.

Has anyone succeeded in creating a workbook/sheet where the columns wrap the text AND the row height is set to auto and the row height adjusts properly when the columns are shrunk down?

Zendog74
  • 65
  • 2
  • 13
  • Not clear where the problem is. Normally `Excel` auto fits the row height automatically while opening the sheet except there is a height explicitly given or the cell is part of a range of merged cells. Surely it will not auto fit the height while you are shrinking the width using the mouse in the GUI. If this is what you want, then there is not a setting for. – Axel Richter Aug 15 '17 at 05:39
  • Thanks @AxelRichter. Unfortunately, yes, I believe what the client is asking for is exactly what you describe: auto-fitting the height of the row when shrinking the column size with the mouse in the GUI. In my testing, Excel WILL auto-fit the row height to two lines of content, but once you get past two lines, it no longer auto-fits the row height. – Zendog74 Aug 15 '17 at 11:43
  • Just doubleclick on the border line below the row number of the row which shall be auto sized in height. For example if height of row 3 shall be auto sized, then doubleclick on the borderline between 3 and 4 in the leftmost row number part of the sheet view. But works only if there are not merged cells involved. – Axel Richter Aug 15 '17 at 12:35
  • Yes, there are several ways to make the row height expand when needed in Excel. The issue is that, in the file being generated by POI, the row height does not automatically expand once the content in a column wraps over two lines. If you create a new spreadsheet and enter the contents manually and turn on word wrap, the row height adjusts automatically, even if the content is over two lines. It seems like there is a setting that is needed in POI when creating the Excel file to make it have this same kind of behavior, but I have been unable to get it to work with anything I have tried so far. – Zendog74 Aug 15 '17 at 13:04
  • "The issue is that, in the file being generated by POI, the row height does not automatically expand once the content in a column wraps over two lines.": Not true in general. Not reproducible for me. Please provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). See https://stackoverflow.com/questions/45387206/how-to-increase-the-height-of-the-excel-row-using-apache-poi-having-merged-cell/45388037#45388037 where exactly the auto-sizing happens in `B1`. – Axel Richter Aug 15 '17 at 13:25
  • Yes, if you get into specific sizing of colums and rows based on the content, I think it can be achieved. We were hoping to avoid having to do that and just use style.wrapText(true) and sheet.autoSizeColumn(i) to achieve dynamic column and row sizing. – Zendog74 Aug 15 '17 at 14:57

0 Answers0