Row height when cell contains carriage returns

Viewed 28

I'm creating a spreadsheet in C# using EPPlus, and some of the cells contain multiline text, with the lines separated by carriage returns. When I open the newly created spreadsheet in Excel, the default row height means that I can only see the first line of text in these cells, e.g.:

enter image description here

After editing that cell (even if I don't actually change anything), Excel automatically adjusts the row height so that the cell can now be seen in full. Note I've added green dots to show where the carriage returns are:

enter image description here

Is there any way to set the row height using EPPlus, so that it looks like the second image when the spreadsheet is first opened in Excel?

Note that I'm also auto-fitting the columns using worksheet.Cells.AutoFitColumns(0);, if that is relevant to the problem.

Edit: I've just made a change whereby I find the cell with the most carriage returns in each row, then multiply the row's height by cell_cr_count + 1. Here, the row height now looks "correct" when I first open the spreadsheet, but weirdly it still only displays the first line of text (until I edit the cell, as before):

enter image description here

Starting to think this is an Excel quirk, and not something that can be fixed using EPPlus?

1 Answers

Ok, so in the end it was fairly simple to fix (isn't it always the way after asking a question on SO?!).

If a cell contains a carriage return then I set WrapText to true using worksheet.Cells[rowNum, colNum].Style.WrapText = true.

In my defence, I had already tried applying this to every cell in the spreadsheet (thinking that it would have no effect on most cells), but it screwed up the sheet to the point where every cell was empty when I opened it in Excel.

Related