Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue#265 #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions FileHelpers/Engines/CsvEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using FileHelpers.Dynamic;
using FileHelpers.Options;

Expand Down Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions FileHelpers/Options/CsvOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]";

/// <summary>A sample file from where to read the field names and number.</summary>
public string SampleFileName
Expand Down Expand Up @@ -161,6 +163,19 @@ public bool IgnoreEmptyLines
set { mIgnoreEmptyLines = value; }
}

/// <summary>Remove special caracters like ;,\t\r\n from string records </summary>
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;
Expand Down