You should have space characters at the start of fields ONLY where the space characters are part of the data. Excel will not strip off leading spaces. You will get unwanted spaces in your headings and data fields. Worse, the " that should be "protecting" that line-break in the third column will be ignored because it is not at the start of the field.
If you have non-ASCII characters (encoded in UTF-8) in the file, you should have a UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file. Otherwise Excel will interpret the data according to your locale's default encoding (e.g. cp1252) instead of utf-8, and your non-ASCII characters will be trashed.
Following comments apply to Excel 2003, 2007 and 2013; not tested on Excel 2000
If you open the file by double-clicking on its name in Windows Explorer, everything works OK.
If you open it from within Excel, the results vary:
- You have only ASCII characters in the file (and no BOM): works.
- You have non-ASCII characters (encoded in UTF-8) in the file, with a UTF-8 BOM at the start: it recognises that your data is encoded in UTF-8 but it ignores the csv extension and drops you into the Text Import not-a-Wizard, unfortunately with the result that you get the line-break problem.
Options include:
- Train the users not to open the files from within Excel :-(
- Consider writing an XLS file directly ... there are packages/libraries available for doing that in Python/Perl/PHP/.NET/etc
You should have space characters at the start of fields ONLY where the space characters are part of the data. Excel will not strip off leading spaces. You will get unwanted spaces in your headings and data fields. Worse, the " that should be "protecting" that line-break in the third column will be ignored because it is not at the start of the field.
If you have non-ASCII characters (encoded in UTF-8) in the file, you should have a UTF-8 BOM (3 bytes, hex EF BB BF) at the start of the file. Otherwise Excel will interpret the data according to your locale's default encoding (e.g. cp1252) instead of utf-8, and your non-ASCII characters will be trashed.
Following comments apply to Excel 2003, 2007 and 2013; not tested on Excel 2000
If you open the file by double-clicking on its name in Windows Explorer, everything works OK.
If you open it from within Excel, the results vary:
- You have only ASCII characters in the file (and no BOM): works.
- You have non-ASCII characters (encoded in UTF-8) in the file, with a UTF-8 BOM at the start: it recognises that your data is encoded in UTF-8 but it ignores the csv extension and drops you into the Text Import not-a-Wizard, unfortunately with the result that you get the line-break problem.
Options include:
- Train the users not to open the files from within Excel :-(
- Consider writing an XLS file directly ... there are packages/libraries available for doing that in Python/Perl/PHP/.NET/etc
After lots of tweaking, here's a configuration that works generating files on Linux, reading on Windows+Excel, though the embedded newline format is not according to the standard:
- Newlines within a field need to be \n (and obviously quoted in double quotes)
- End of record: \r\n
- Make sure that you don't start a field with equals, otherwise it gets treated as a formula and truncated
In Perl, I used Text::CSV to do this as follows:
use Text::CSV;
open my $FO, ">:encoding(utf8)", $filename or die "Cannot create $filename:
csv = Text::CSV->new({ binary => 1, eol => "\r\n" });
#for each row...:
$csv -> print ($FO, \@row);
- Open a new blank workbook.
- Click the Sheet1 name tab and choose View Code.
- When the VBE opens, paste the following into the code sheet titled Sheet1 (Code).
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Parent.UsedRange.Cells.Count = 1 And _
Target.Cells.Count = 1 And _
Target.Address(0, 0) = "A1" Then
On Error GoTo safe_exit
Application.EnableEvents = False
Dim a As Long, b As Long, arr1 As Variant, arr2 As Variant, hdr As Variant, tmp As Variant
arr1 = Split(Replace(Target.Value2, ",Name:", vbLf & "Name:"), vbLf)
For a = LBound(arr1) To UBound(arr1)
arr2 = Split(arr1(a), Chr(44))
For b = LBound(arr2) To UBound(arr2)
tmp = Split(arr2(b), Chr(58))
hdr = Application.Match(tmp(0), Rows(1), 0)
If IsError(hdr) Then
Cells(1, Columns.Count).End(xlToLeft).Offset(0, 1) = tmp(0)
hdr = Application.Match(tmp(0), Rows(1), 0)
End If
Cells(2 + a, hdr) = tmp(1)
Next b
Next a
Columns(1).EntireColumn.Delete
End If
safe_exit:
Application.EnableEvents = True
End Sub
- Tap Ctrl+Q to return to your new, blank worksheet. - Open the TXT file in notepad or some other text editor and copy the whole thing. - Go back to your new, blank Sheet1 and paste the text into A1.

As I said in my comments, you can replace ,Name: with \nName: (new line/carriage return).
This is very easy to do in Notepad++ (a free program)
https://notepad-plus-plus.org/
Make sure you set the Search Mode to Extended
Use Find: ,Name:
Replace With: \nName:

It comes over looking like this when you set it to Delimited using Comma

I struggled with this as well but heres the solution. If you add " before and at the end of the csv string you are trying to display, it will consolidate them into 1 cell while honoring new line.
csvString += "\""+"Date Generated: \n" ;
csvString += "Doctor: " + "\n"+"\"" + "\n";
This question was answered well at Can you encode CR/LF in into CSV files?.
Consider also reverse engineering multiple lines in Excel. To embed a newline in an Excel cell, press Alt+Enter. Then save the file as a .csv. You'll see that the double-quotes start on one line and each new line in the file is considered an embedded newline in the cell.