Excel CSV to string[]

While building a Excel CSV to Claim application for a client I needed to take a comma delimited record and place it into a string array.

The challenge is that there is a wrap character only for the fields that have the delimiter in the data.

Example:

012346,012346001,SMITH              ,SCOTT,J
012345,012345001,”WILLIAMS, III                 ”,HENRY                 ,R
012344,012344001,DAHL                ,MICHELLE                      ,T

I could not find an easy way to parse this into an array, this is what I came up with:

public string[] Split(string expression, string delimiter, string qualifier, bool ignoreCase)
{
    string _Statement = String.Format("{0}(?=(?:[^{1}]*{1}[^{1}]*{1})*(?![^{1}]*{1}))",Regex.Escape(delimiter), Regex.Escape(qualifier));

    RegexOptions _Options = RegexOptions.Compiled | RegexOptions.Multiline;
    if (ignoreCase) _Options = _Options | RegexOptions.IgnoreCase;

    Regex _Expression = new Regex(_Statement, _Options);
    ArrayList finalresult=new ArrayList();
    foreach (string result in _Expression.Split(expression))
    {
        string tempData=result.Replace(qualifier, "");
        finalresult.Add(tempData.Trim());
    }
    string[] strArray = finalresult.ToArray(Type.GetType("System.String")) as string[];
    return strArray;
}

I am sure it can be done better, yes, I know, two passes through isn’t quite the best to remove the “ from the quoted field, and I could have done it a little better to remove the spaces, but it does what I needed, and the client was not paying for how fast it runs, he was paying me for how fast I could code it!

posted @ Thursday, July 23, 2009 3:40 AM

Print

Comments on this entry:

 re: Excel CSV to string[]

Left by Bhola meena[MSFT] at 7/27/2009 8:37 PM

It can be done in one pass. You need to create a Regex and parse the input string.

See below example:
string input = @"012346,012346001,SMITH ,SCOTT,J
012345,012345001,”WILLIAMS, III ”,HENRY ,R
012344,012344001,DAHL ,MICHELLE";

Regex reg = new Regex("”[^”]*” | [^,]+ ", RegexOptions.IgnorePatternWhitespace | RegexOptions.Multiline);
List<string> strArray = new List<string>();
foreach (Match m in reg.Matches(input))
{
Console.WriteLine("Match:{0}", m.Value);
strArray.Add(m.Value);
}

# re: Excel CSV to string[]

Left by software development london at 8/20/2009 8:03 AM
Gravatar

Interesting,

reg expression can make this task more easier Although I am not the fan of reg becouse it's so hard to write one

Anyway, thanks for the post

Your comment:



 (will not be displayed)


 
 
 
Please add 4 and 7 and type the answer here:
 

Live Comment Preview:

 
«March»
SunMonTueWedThuFriSat
28123456
78910111213
14151617181920
21222324252627
28293031123
45678910