Excel how to fit text in cell, based on cell witdh

2.7k views Asked by At

I want to set the witdh of a cell in my ExcelSheet.

Set the witdh:

worksheet.Columns["K"].ColumnWidth = 114.80;

When the text is larger then the ColumnWith the text is not visible.

I want to split the text to a new row in the same cell based on the ColumnWith.

I tried to add \r\n to the string in the Excel but no result.

EDIT after answers

This works perfectly:

 worksheet.Columns["K"].ColumnWidth = 114;
 Excel.Range rangeK = worksheet.get_Range("K1");
 rangeK.EntireColumn.WrapText = true;
2

There are 2 answers

0
Zhertal On BEST ANSWER

What you are looking for is this:

worksheet.Range("K1:K100").WrapText = True;

That code, for example, will set the cells from K1 to K100 to wrap the contents inside their cells.

0
Nathan M. On

You have a couple options. The first option (which is what I would suggest) is to autoresize the columns.

worksheet.Columns.AutoFit();

The next option is to word wrap all text, which I have not done but this link might be of use to you. I hope this helps.