'Using ClosedXML how to adjust row height to content?

I create cell with text. After that I set WrapText property and column width.

var cell = worksheet.Cell("A1");
cell.Style.Alignment.WrapText = true;
cell.SetValue("This is very long text");
worksheet.Column(1).Width = 10;
worksheet.Rows().AdjustToContents();

The text has been moved by words, but row height is not changed. How to adjust row height to cell content?



Solution 1:[1]

There are many ways to achieve this.

Don't use wrap or shrink properties on cell values rather include this line just before saving your excel

ws.Columns().AdjustToContents();

Another way is to make use of Allignment property

 IXLRange titleRange = ws.Range("B2:AA2");
        titleRange.Cells().Style
            .Alignment.SetWrapText(true); // Its single statement

Hope it helps!!

Solution 2:[2]

The following code worked for me.

IXLRange contents = ws.Range("A1:A50");
contents.Style.Alignment.WrapText = true;

Solution 3:[3]

You can also AdjustToContents on Specific range of cells.

worksheet.Columns(2, 20).AdjustToContents();

Solution 4:[4]

I think this is a bug of ClosedXML. enter link description here

ws.Row(1).AdjustToContents();
ws.Row(1).ClearHeight();

In my case it works. Version: 0.95.4

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hardik B Bhilota
Solution 2 Dhruv Rangunwala
Solution 3 Smit Patel
Solution 4 ouflak