Like Maciej mentioned in the comment to the question you need to replace linenbreaks (linefeeds resp carriage linefeeds) with
<br>
But it is not only a simple
<br>
you have to add. To inform Excel to keep it in the same cell you need to replace linebreaks with
<br style='mso-data-placement: same-cell'>
For examlpe
<td>Jan<br style='mso-data-placement: same-cell'>uary</td>
I hope it helps.
[Edit1]
Finally a better option with 'embedded' style
<style>
br {mso-data-placement:same-cell;}
</style>
<table>
<tr>
<th>Col 1</th>
<th>Col 2</th>
</tr>
<tr>
<td>Line1 <br>Line 2</td>
<td>data for col 2</td>
</tr>
</table>
[Edit2]
To allow to have <br> and also something like <br> in the source text you need to encode the source text with
HtmlEncode
. More info about
HtmlEncode
you will find e.g. here
WebUtility.HtmlEncode Method (System.Net) | Microsoft Docs[
^]
[Edit 3]
Yet another thing. In case there is a chance that a lot of data are in your grid you should think about using
StringBuilder
instead of concatenating strings.
I remember a test I made before about 10 years.
Concatenating 50'000 times a string of 10 characters in a loop took 25 Sec. while with StringBuilder it took '0 Seconds' (measured with Environement.TickCount).