Using ClosedXML how to adjust row height to content?
Asked Answered
J

5

23

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?

Jeanelle answered 14/5, 2014 at 7:42 Comment(0)
M
16

It works when you remove the worksheet.Rows().AdjustToContents();.

Autofitting sometimes needs more of a trial and error approach ...

Meryl answered 15/5, 2014 at 15:24 Comment(0)
P
19

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!!

Presentable answered 15/7, 2015 at 17:38 Comment(0)
M
16

It works when you remove the worksheet.Rows().AdjustToContents();.

Autofitting sometimes needs more of a trial and error approach ...

Meryl answered 15/5, 2014 at 15:24 Comment(0)
F
5

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

Formulism answered 8/2, 2022 at 9:40 Comment(0)
M
1

The following code worked for me.

IXLRange contents = ws.Range("A1:A50");
contents.Style.Alignment.WrapText = true;
Manutius answered 7/10, 2015 at 16:9 Comment(0)
D
-1

You can also AdjustToContents on Specific range of cells for column level contents.

worksheet.Columns(2, 20).AdjustToContents();
Dissuasive answered 19/10, 2015 at 17:9 Comment(4)
Question is about adjusting the height, not columns.Poteat
7 Years later????? - I didn't remember the question :DDissuasive
@SmitPatel In that case, it's best if you delete your answer, since it doesn't answer the question! It adds nothing new that the other (more upvoted) answers haven't already covered.Erickson
@Erickson - I've put it because of a Range can also be applied with AdjustToContents - Someone might have that question as well apart from what asked, As a developer we should think on wider level and don't need to stick to the exact question everytime.Dissuasive

© 2022 - 2024 — McMap. All rights reserved.