diff --git a/CsvExport.cs b/CsvExport.cs index a82977b..73bb44d 100644 --- a/CsvExport.cs +++ b/CsvExport.cs @@ -45,6 +45,34 @@ public class CsvExport /// Dictionary currentRow { get { return rows[rows.Count - 1]; } } + /// + /// The string used to separate columns in the output + /// + 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. + /// + /// + /// The string used to separate columns in the output. + /// By default this is a comma so that the generated output is a CSV file. + /// + /// + /// 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; + } + /// /// Set a value on this column /// @@ -92,7 +120,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 ""; @@ -103,7 +135,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 +158,10 @@ public static string MakeValueCsvFriendly(object value) /// private IEnumerable ExportToLines() { - yield return "sep=,"; + if (includeColumnSeparatorDefinitionPreamble) 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 +170,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], columnSeparator))); } }