简体   繁体   中英

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?

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

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

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

The following code worked for me.

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

您还可以在特定范围的单元格上调整ToTteContents。

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM