The escape sequence \n' is a linefeed which maps to decimal 10 so
((char)10) == '\n'` is the same thing. All your code is doing is replacing the more traditional CRLF with just a LF.
I'm a little confused on the code you posted. That looks like code that you're using to manipulate the UI GridView and not the Excel data itself. Changing things in the UI isn't going to impact the Excel file at all. To change the data in the Excel file you'll need to transform the data while writing the Excel file.
Here's an example of how you might open an Excel spreadsheet, enumerate the rows and modify the data in the second column to replace spaces with a newline such that Excel shows it on two separate lines.
var app = new Excel.Application();
Excel.Workbook workbook = null;
try
{
workbook = app.Workbooks.Open(path);
Excel.Worksheet sheet = workbook.ActiveSheet;
foreach (Excel.Range row in sheet.UsedRange)
{
var oldValue = row.Cells[1, 2].Value;
var newValue = (oldValue?.ToString() ?? "").Replace(" ", "\n");
row.Cells[1, 2].Value = newValue;
};
workbook.Save();
} finally
{
workbook.Close();
app.Quit();
};
I'm using Excel interop here but the flow is basically the same. I'm not sure how you're generating the Excel file but at some point you'll be extracting the data from the UI (assuming you don't already have it stored in memory somewhere) and it is at that point that you'll want to convert the text to using newlines (\n
).