Here’s how you can read a CSV file using Regular Expressions in .Net:
public static DataTable GetDataTableFromCsvFile(string file)
{
// Where the CSV data goes
DataTable dt = new DataTable("CsvData");
// The pattern used to parse the CSV
const string csvPattern = ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))";
Regex csvRegex = new Regex(csvPattern);
// Read all lines in the file
// (not great for large files)
string[] fileLines = File.ReadAllLines(file);
// Get the column headers
// (assumes first row has headers and that
// each column contains string values and
// that each column name is unique)
Dictionary<int, string> headers = new Dictionary<int, string>();
string[] headerValues = csvRegex.Split(fileLines[0]);
for (int i = 0; i < headerValues.Length; i++)
{
headers.Add(i, headerValues[i]);
dt.Columns.Add(new DataColumn(headerValues[i], typeof (string)));
}
// Then add the the rest of the lines
for (int k = 1; k < fileLines.Length; k++)
{
DataRow dr = dt.NewRow();
string line = fileLines[k];
string[] cols = csvRegex.Split(line);
for (int i = 0; i < cols.Length; i++)
{
string header = headers[i];
string data = cols[i];
// remove quotes around the field
if (data.Length > 1 && data.StartsWith("\"") && data.EndsWith("\""))
data = data.Remove(data.Length - 1, 1).Remove(0, 1);
dr[header] = data;
}
dt.Rows.Add(dr);
}
return dt;
}
Of course, you will need to add in error catching and handling as well.
Hope this helps!