diff --git a/FileHelpers/Engines/CsvEngine.cs b/FileHelpers/Engines/CsvEngine.cs index b08932e04..5deebc804 100644 --- a/FileHelpers/Engines/CsvEngine.cs +++ b/FileHelpers/Engines/CsvEngine.cs @@ -3,6 +3,7 @@ using System.Data; using System.Diagnostics; using System.IO; +using System.Text.RegularExpressions; using FileHelpers.Dynamic; using FileHelpers.Options; @@ -133,6 +134,16 @@ public static void DataTableToCsv(DataTable dt, string filename, CsvOptions opti } foreach (DataRow dr in dt.Rows) { + if (options.IgnoreSpecialCharacters) + { + for (int i = 0; i < dt.Columns.Count; i++) + { + if (dt.Columns[i].DataType == typeof(string) && dr[i] != DBNull.Value) + { + dr[i] = Regex.Replace((string)dr[i], options.Separators, " "); + } + } + } object[] fields = dr.ItemArray; Append(fs, options, fields); } diff --git a/FileHelpers/Options/CsvOptions.cs b/FileHelpers/Options/CsvOptions.cs index ea68802cf..5ff62a32a 100644 --- a/FileHelpers/Options/CsvOptions.cs +++ b/FileHelpers/Options/CsvOptions.cs @@ -63,6 +63,8 @@ public CsvOptions(string className, char delimiter, char headerDelimiter, string private Encoding mEncoding = Encoding.GetEncoding(0); private bool mIgnoreEmptyLines = false; private bool mIncludeHeaderNames; + private bool mIgnoreSpecialCharacters; + private string mSeparators = @"[;,\t\r\n]"; /// A sample file from where to read the field names and number. public string SampleFileName @@ -161,6 +163,19 @@ public bool IgnoreEmptyLines set { mIgnoreEmptyLines = value; } } + /// Remove special caracters like ;,\t\r\n from string records + public bool IgnoreSpecialCharacters + { + get { return mIgnoreSpecialCharacters; } + set { mIgnoreSpecialCharacters = value; } + } + + public string Separators + { + get { return mSeparators; } + set { mSeparators = value; } + } + private ConvertHelpers.DecimalConverter mDecimalConv; private ConvertHelpers.DoubleConverter mDoubleConv; private ConvertHelpers.SingleConverter mSingleConv;