Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ClosedXML how to adjust row height to content?

Tags:

c#

closedxml

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?

like image 232
Vlad Avatar asked May 14 '14 07:05

Vlad


People also ask

How do you adjust the height of a row to automatically fit the contents?

Change the row height to fit the contents Select the row or rows that you want to change. On the Home tab, in the Cells group, click Format. Under Cell Size, click AutoFit Row Height.

Which options allows you to change the row height?

Click the Home tab. in the Cells group, click on the Format option. In the dropdown, click on the Row Height option. In the Row Height dialog box, enter the height that you want for each of these selected rows.

How do I change the width and height of a row?

Select a row or a range of rows. On the Home tab, select Format > Row Width (or Row Height). Type the row width and select OK.


1 Answers

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

like image 148
Hardik B Bhilota Avatar answered Oct 08 '22 11:10

Hardik B Bhilota