How to show line break in excel c#

1.6k views Asked by At

I am using ExcelPackage to generate excel in C# application. String has new line character in it but excel doesn't show it by default. When I click on cell twice then only it shows new line otherwise whole text display in single line. Below is the data string.

Today is Monday.\r\nTomorrow is Tuesday.

When I open excel after downloading it. This is how it show up. enter image description here

On double click excel shows content properly. I tried wrap property on cell in C# code but it didn't help. How can I make it work.

edit: added screen shot with wrap property to true in C#.enter image description here

1

There are 1 answers

8
AbuDawood Oussama On

I am writing a quick answer without prior tests, but I assume it will work for you. If you can't see line breaks except when you double click on the cell this means that the text wrapping option is not enabled. You can check this by accessing Right_click > Format Cells > Alignment. and verify if the Wrap text is ticked.

if not, so this means that your code does not do this for you, and there are many expected reasons why:

  • you didn't enable it

    worksheet.Cells["A1:A2"].Style.WrapText = true;  //https://stackoverflow.com/a/29737544/2514158
    
  • You are targetting the wrong cell