CSV Reader in C#

Typically, Importing data (contact list) from microsoft outlook to our application is necessary in almost every data driven application. The following examples shows the easy way to implement this functionality in C#.
This class can be used in internationalization. ie. it can read unicode character also.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Text;
using System.IO;

namespace CSVReader
{
public class CsvParser
{
public static DataTable Parse(string data, bool headers)
{
return Parse(new StringReader(data), headers);
}

public static DataTable Parse(string data)
{
return Parse(new StringReader(data));
}

public static DataTable Parse(TextReader stream)
{
return Parse(stream, false);
}

public static DataTable Parse(TextReader stream, bool headers)
{
DataTable table = new DataTable();
CsvStream csv = new CsvStream(stream);
string[] row = csv.GetNextRow();
if (row == null)
return null;
if (headers)
{
foreach (string header in row)
{
if (header != null && header.Length > 0 && !table.Columns.Contains(header))
{
string newheader = parseString(header, @",.#;>/\");
table.Columns.Add(newheader, typeof(string));
}
else
table.Columns.Add(GetNextColumnHeader(table), typeof(string));
}
row = csv.GetNextRow();
}
while (row != null)
{
while (row.Length > table.Columns.Count)
table.Columns.Add(GetNextColumnHeader(table), typeof(string));
table.Rows.Add(row);
row = csv.GetNextRow();
}
return table;
}

// parseString will find unnecessary character's from the column header and removes it.
private static string parseString(string header, string delim)
{
//string x=header;
for (int i = 0; i <> 0 && header.IndexOf('/') < header =" header.Replace(delim[i].ToString()," c =" 1;" h = "Column" stream =" s;" row =" new" item =" GetNextItem();" item ="=" count ="=" eos =" false;" eol =" false;" eol =" false;" quoted =" false;" predata =" true;" postdata =" false;" item =" new" c =" GetNextChar(true);"> 0 ? item.ToString() : null;

if ((postdata || !quoted) && c == ',')
// end of item, return
return item.ToString();

if ((predata || postdata || !quoted) && (c == '\x0A' || c == '\x0D'))
{
// we are at the end of the line, eat newline characters and exit
EOL = true;
if (c == '\x0D' && GetNextChar(false) == '\x0A')
// new line sequence is 0D0A
GetNextChar(true);
return item.ToString();
}

if (predata && c == ' ')
// whitespace preceeding data, discard
continue;

if (predata && c == '"')
{
// quoted data is starting
quoted = true;
predata = false;
continue;
}

if (predata)
{
// data is starting without quotes
predata = false;
item.Append(c);
continue;
}

if (c == '"' && quoted)
{
if (GetNextChar(false) == '"')
// double quotes within quoted string means add a quote
item.Append(GetNextChar(true));
else
// end-quote reached
postdata = true;
continue;
}

// all cases covered, character must be data
item.Append(c);
}
}

private char[] buffer = new char[4096];
private int pos = 0;
private int length = 0;

private char GetNextChar(bool eat)
{
if (pos >= length)
{
length = stream.ReadBlock(buffer, 0, buffer.Length);
if (length == 0)
{
EOS = true;
return '\0';
}
pos = 0;
}
if (eat)
return buffer[pos++];
else
return buffer[pos];
}
}

}
}

Comments

Popular posts from this blog

Fetching Address book AKA Contact information from Device using Xamarin.Forms

Custom Attributes in MVC

Partial view and AJAX Form with MVC3 Razor