From 5564485a9e507dd76096697ac9869581b683ea98 Mon Sep 17 00:00:00 2001 From: Haruyasu Ueda Date: Thu, 18 Apr 2019 21:12:11 +0900 Subject: [PATCH 1/2] Fix for #8 - No replacement about \r \n, instead the column is wrapped by "". - A doublequote is replaced with two doublequotes. - No commna replaced because it is not necessary. --- XstFile.cs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/XstFile.cs b/XstFile.cs index c79f5a5..ecf9fee 100644 --- a/XstFile.cs +++ b/XstFile.cs @@ -476,14 +476,11 @@ private void AddCsvValue(StringBuilder sb, string value, ref bool hasValue) if (value != null) { - value = value.Replace("\r\n", "; ").Replace("\r", "; ").Replace("\n", "; "); - if (value.Contains(',')) + if (value.Contains(',') || value.Contains('"') || value.Contains("\n") || value.Contains("\n") ) { // We need to quote the value, and therefore get rid of quotes in it // Excel is also fooled by spaces after embedded commas - var val = value.Replace("\"", "'"); - while (val.Contains(", ")) - val = val.Replace(", ", ","); + var val = value.Replace("\"", "\"\""); sb.Append("\""); sb.Append(EnforceCsvValueLengthLimit(val)); sb.Append("\""); From fa0cd289b94d5c7133c2922a5e8699f1a19042e2 Mon Sep 17 00:00:00 2001 From: Haruyasu Ueda Date: Thu, 9 May 2019 13:05:53 +0900 Subject: [PATCH 2/2] care about multilingual character use utf-8 encoding because de fuct standard. quotation is necessary around utf-8 character, so always add quotation mark. --- XstFile.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/XstFile.cs b/XstFile.cs index ecf9fee..3a1e7bd 100644 --- a/XstFile.cs +++ b/XstFile.cs @@ -362,7 +362,8 @@ public void ExportMessageProperties(IEnumerable messages, string fileNa var columns = dict.Keys.OrderBy(x => x).ToArray(); // And finally output the CSV file line by line - using (var sw = new System.IO.StreamWriter(fileName, false)) + using (var sw = new System.IO.StreamWriter(fileName, false, /* Encoding.Default */ System.Text.Encoding.GetEncoding("utf-8") )) + { StringBuilder sb = new StringBuilder(); bool hasValue = false; @@ -476,7 +477,8 @@ private void AddCsvValue(StringBuilder sb, string value, ref bool hasValue) if (value != null) { - if (value.Contains(',') || value.Contains('"') || value.Contains("\n") || value.Contains("\n") ) + // multilingual character should be quoted, so almost always quotation is necessary + // if (value.Contains(',') || value.Contains('"') || value.Contains("\n") || value.Contains("\n") ) { // We need to quote the value, and therefore get rid of quotes in it // Excel is also fooled by spaces after embedded commas @@ -485,8 +487,8 @@ private void AddCsvValue(StringBuilder sb, string value, ref bool hasValue) sb.Append(EnforceCsvValueLengthLimit(val)); sb.Append("\""); } - else - sb.Append(EnforceCsvValueLengthLimit(value)); + // else + // sb.Append(EnforceCsvValueLengthLimit(value)); } hasValue = true;