JP..t
JP..t

Reputation: 615

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

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;

Upvotes: 1

Views: 2943

Answers (2)

Zhertal
Zhertal

Reputation: 425

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.

Upvotes: 2

Nathan M.
Nathan M.

Reputation: 286

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.

Upvotes: 2

Related Questions