1

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

Anil
  • 1,669
  • 5
  • 19
  • 44

1 Answers1

0

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

AbuDawood
  • 745
  • 7
  • 22
  • 1
    I had tried wrap property but it break text based on cell size not based on line break. – Anil Aug 11 '20 at 00:21
  • please confirm first that in the derived file the wrap text is checked before we go thinking in any other prob – AbuDawood Aug 11 '20 at 00:23
  • This screenshot you are sharing is a clear sign that the Wrap text option is not enabled! , You should check first by right click on the targeted cell and see. let me know after – AbuDawood Aug 11 '20 at 00:27
  • Yes wrap text is checked. I verified it again. – Anil Aug 11 '20 at 00:27
  • screen shot is after I removed the wrap text property because it was not working. As wrap was breaking text based on cell size , content was not looking good so I removed it. – Anil Aug 11 '20 at 00:28
  • Okay, let's make our doubt that the text due to some reason didn't include the correct line break code. Could please write some text with line break in some word file and copy past to the cell ? – AbuDawood Aug 11 '20 at 00:29
  • ok so added another screen shot. I hope it clarifies that text has line break and wrap is checked. – Anil Aug 11 '20 at 00:33
  • It is clear now, You should know that the column width option is stronger than the wrap text option. If you want to confirm that you just need to increase the column width by sliding its right corner. You need to programmatically resize the column width if you see this is bothersome. However, technically it is correct! – AbuDawood Aug 11 '20 at 00:38