Skip to content

Commit

Permalink
Use culture specific comma when exporting csv files
Browse files Browse the repository at this point in the history
  • Loading branch information
Dijji committed May 21, 2020
1 parent 7938c52 commit e5ab96f
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions XstFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -384,7 +385,7 @@ public void ExportMessageProperties(IEnumerable<Message> 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, /* Encoding.Default */ System.Text.Encoding.GetEncoding("utf-8") ))
using (var sw = new System.IO.StreamWriter(fileName, false, Encoding.UTF8))

{
StringBuilder sb = new StringBuilder();
Expand Down Expand Up @@ -498,22 +499,16 @@ private void ReadMessageTables(FileStream fs, BTree<Node> subNodeTree, Message m
private void AddCsvValue(StringBuilder sb, string value, ref bool hasValue)
{
if (hasValue)
sb.Append(",");
sb.Append(CultureInfo.CurrentCulture.TextInfo.ListSeparator); // aka comma

if (value != null)
{
// 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
var val = value.Replace("\"", "\"\"");
sb.Append("\"");
sb.Append(EnforceCsvValueLengthLimit(val));
sb.Append("\"");
}
// else
// sb.Append(EnforceCsvValueLengthLimit(value));
// 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("\"", "\"\"");
sb.Append("\"");
sb.Append(EnforceCsvValueLengthLimit(val));
sb.Append("\"");
}

hasValue = true;
Expand Down

0 comments on commit e5ab96f

Please sign in to comment.