Skip to content

Commit

Permalink
Excel does not handle embedded new line characters well in CSV files …
Browse files Browse the repository at this point in the history
…encoded with UTF-8, so remove them (see #10)
  • Loading branch information
Dijji committed Dec 13, 2020
1 parent 5eb3d3c commit 304ecac
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions XstFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -515,9 +515,11 @@ private void AddCsvValue(StringBuilder sb, string value, ref bool hasValue)

if (value != null)
{
// Multilingual characters should be quoted, so We will just quote all values,
// Multilingual characters should be quoted, so we will just quote all values,
// which means we need to double quotes in the value
var val = value.Replace("\"", "\"\"");
// Excel cannot cope with Unicode files with values containing
// new line characters, so remove those as well
var val = value.Replace("\"", "\"\"").Replace("\r\n", "; ").Replace("\r", " ").Replace("\n", " ");
sb.Append("\"");
sb.Append(EnforceCsvValueLengthLimit(val));
sb.Append("\"");
Expand Down

2 comments on commit 304ecac

@flywire
Copy link
Contributor

@flywire flywire commented on 304ecac Jan 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is a good idea. You can enter a newline in an excel cell with <alt><enter>.

@Dijji
Copy link
Owner Author

@Dijji Dijji commented on 304ecac Jan 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What you say is true. But you can't get that new line in there by importing it from a CSV file. The recognised way to do it is to embed the new line in double quotes. Indeed, if you import such a CSV into OpenOffice, you get what we want. Unfortunately, if you import it into Excel, what you get is a new row, and a big mess in the case of exported property files. So I have sacrificed some fidelity in the values for the sake of being able to open the exported CSV files in Excel with substantially correct results.

Please sign in to comment.