From 4db76fa713b7e5c1c8367d03b96af0312c57ac73 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 24 Oct 2016 21:59:52 -0700 Subject: [PATCH 1/3] Extract column separator constant --- CsvExport.cs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/CsvExport.cs b/CsvExport.cs index a82977b..2570d65 100644 --- a/CsvExport.cs +++ b/CsvExport.cs @@ -45,6 +45,11 @@ public class CsvExport /// Dictionary currentRow { get { return rows[rows.Count - 1]; } } + /// + /// The string used to separate columns in the exported CSV + /// + private const string columnSeparator = ","; + /// /// Set a value on this column /// @@ -103,7 +108,7 @@ public static string MakeValueCsvFriendly(object value) return ((DateTime)value).ToString("yyyy-MM-dd HH:mm:ss"); } string output = value.ToString().Trim(); - if (output.Contains(",") || output.Contains("\"") || output.Contains("\n") || output.Contains("\r")) + if (output.Contains(columnSeparator) || output.Contains("\"") || output.Contains("\n") || output.Contains("\r")) output = '"' + output.Replace("\"", "\"\"") + '"'; if (output.Length > 30000) //cropping value for stupid Excel @@ -126,10 +131,10 @@ public static string MakeValueCsvFriendly(object value) /// private IEnumerable ExportToLines() { - yield return "sep=,"; + yield return "sep=" + columnSeparator; // The header - yield return string.Join(",", fields); + yield return string.Join(columnSeparator, fields); // The rows foreach (Dictionary row in rows) @@ -138,7 +143,7 @@ private IEnumerable ExportToLines() { row[k] = null; } - yield return string.Join(",", fields.Select(field => MakeValueCsvFriendly(row[field]))); + yield return string.Join(columnSeparator, fields.Select(field => MakeValueCsvFriendly(row[field]))); } } From 2e2db90cfb055ad0dee799aa0ea806159b0e56b8 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 24 Oct 2016 22:07:39 -0700 Subject: [PATCH 2/3] Make column separator configurable This is a partial fix for #9 --- CsvExport.cs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/CsvExport.cs b/CsvExport.cs index 2570d65..80f8cac 100644 --- a/CsvExport.cs +++ b/CsvExport.cs @@ -46,9 +46,21 @@ public class CsvExport Dictionary currentRow { get { return rows[rows.Count - 1]; } } /// - /// The string used to separate columns in the exported CSV + /// The string used to separate columns in the output /// - private const string columnSeparator = ","; + private readonly string columnSeparator; + + /// + /// Initializes a new instance of the class. + /// + /// + /// The string used to separate columns in the output. + /// By default this is a comma so that the generated output is a CSV file. + /// + public CsvExport(string columnSeparator=",") + { + this.columnSeparator = columnSeparator; + } /// /// Set a value on this column @@ -97,7 +109,11 @@ public void AddRows(IEnumerable list) /// Also if it contains any double quotes ("), then they need to be replaced with quad quotes[sic] ("") /// Eg "Dangerous Dan" McGrew -> """Dangerous Dan"" McGrew" /// - public static string MakeValueCsvFriendly(object value) + /// + /// The string used to separate columns in the output. + /// By default this is a comma so that the generated output is a CSV document. + /// + public static string MakeValueCsvFriendly(object value, string columnSeparator=",") { if (value == null) return ""; if (value is INullable && ((INullable)value).IsNull) return ""; @@ -143,7 +159,7 @@ private IEnumerable ExportToLines() { row[k] = null; } - yield return string.Join(columnSeparator, fields.Select(field => MakeValueCsvFriendly(row[field]))); + yield return string.Join(columnSeparator, fields.Select(field => MakeValueCsvFriendly(row[field], columnSeparator))); } } From 4fde285dac818e7c1637cab5ebc871eb8390f910 Mon Sep 17 00:00:00 2001 From: Clemens Wolff Date: Mon, 24 Oct 2016 22:19:12 -0700 Subject: [PATCH 3/3] Add flag to suppress the `sep=...` preamble This is a partial fix for #9 --- CsvExport.cs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/CsvExport.cs b/CsvExport.cs index 80f8cac..73bb44d 100644 --- a/CsvExport.cs +++ b/CsvExport.cs @@ -50,6 +50,11 @@ public class CsvExport /// private readonly string columnSeparator; + /// + /// Whether to include the preamble that declares which column separator is used in the output + /// + private readonly bool includeColumnSeparatorDefinitionPreamble; + /// /// Initializes a new instance of the class. /// @@ -57,9 +62,15 @@ public class CsvExport /// The string used to separate columns in the output. /// By default this is a comma so that the generated output is a CSV file. /// - public CsvExport(string columnSeparator=",") + /// + /// Whether to include the preamble that declares which column separator is used in the output. + /// By default this is true so that Excel can open the generated CSV + /// without asking the user to specify the delimiter used in the file. + /// + public CsvExport(string columnSeparator=",", bool includeColumnSeparatorDefinitionPreamble=true) { this.columnSeparator = columnSeparator; + this.includeColumnSeparatorDefinitionPreamble = includeColumnSeparatorDefinitionPreamble; } /// @@ -147,7 +158,7 @@ public static string MakeValueCsvFriendly(object value, string columnSeparator=" /// private IEnumerable ExportToLines() { - yield return "sep=" + columnSeparator; + if (includeColumnSeparatorDefinitionPreamble) yield return "sep=" + columnSeparator; // The header yield return string.Join(columnSeparator, fields);