Title: Create CSV Reader - CSV Parser and Load Dataset from Comma Separated Values TextFile using TextFieldParser (Microsoft.VisualBasic.FileIO.TextFieldParser in C#) Author: Greg Dubinovskiy Email: [email protected] Environment: c# .NET Keywords: Load Dataset from CSV File, CSV Reader, CSV PArser, Open Dataset from Text File, Load ListView, Load GridView, TextFile, System.Data.OleDb Namespace, OledbConnection, OleDbCommand Level: Intermediate Description: Create CSV Reader - CSV Parser and Load Dataset from Comma Separated Values TextFile using TextFieldParser (Microsoft.VisualBasic.FileIO.TextFieldParser in C#) Section Miscellaneous SubSection General
The article presented bellow, shows how to open Dataset from a CSV (Comma Separated Values) Text File using
TextFieldParser (Microsoft.VisualBasic.FileIO.TextFieldParser in C#).
(see
Siccolo Artciles - How to Open Dataset from Text File using OleDB and OleDbConnection)
(see
Siccolo miscellaneous development tips - How to Open Dataset from Text File) .
In one of my recent projects, I needed to allow a user to upload Outlook and/or Outlook Express Contacts CSV file, and display it on the page.
(to allow user file upload, I used <asp:FileUpload />
control). I did not want to save uploaded CSV file for processing. Instead, I wanted to read it,
parse it and display it (see
Loaded Dataset from Text File using OleDB and OleDbConnection) .
To accomplish such, I wanted to be able open CSV text file, read, parse it, and load Dataset from opened CSV file, so I can manupulate rows and columns easily.
Because I didn't want to save the CSV file, I cound't use OleDbConnection
to open Dataset from OleDB-connected CSV file. Instead, I populated Dataset manually using
TextFieldParser Class
:
TextFieldParser Class
: - Provides methods and properties for parsing structured text files.
Parsing a text file with the TextFieldParser is similar to iterating over a text file, while the ReadFields method to extract fields of text is similar to splitting the strings.
The TextFieldParser can parse two types of files: delimited or fixed-width. Some properties, such as Delimiters and HasFieldsEnclosedInQuotes are meaningful only when working with delimited files, while the FieldWidths property is meaningful only when working with fixed-width files.
using Microsoft.VisualBasic.FileIO;
private bool ProcessOutlookCSVFile(Stream inputStream,
string csvType,
out string contactList,
out string errorInfo)
{
contactList = String.Empty;
errorInfo = String.Empty;
try
{
Microsoft.VisualBasic.FileIO.TextFieldParser parser =
new TextFieldParser(inputStream);
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
//convert to dataset:
DataSet contactsData = new DataSet("Contacts");
contactsData.Tables.Add("ContactList");
String[] stringRow = parser.ReadFields();
foreach (String field in stringRow)
{
contactsData.Tables[0].Columns.Add(field, Type.GetType("System.String"));
}
//populate with data:
while (!parser.EndOfData)
{
stringRow = parser.ReadFields();
contactsData.Tables[0].Rows.Add(stringRow);
}
parser.Close();
if (csvType == "Outlook")
{
foreach (DataRow dr in contactsData.Tables[0].Rows)
{
if (String.IsNullOrEmpty(dr["E-mail Address"].ToString())) { continue; }
if (contactList != String.Empty) { contactList += ";"; }
contactList += dr["Last Name"].ToString() + ", " + dr["First Name"].ToString();
contactList += "|" + dr["E-mail Address"].ToString();
}
}
if (csvType == "OutlookExpress")
{
foreach (DataRow dr in contactsData.Tables[0].Rows)
{
if (String.IsNullOrEmpty(dr["E-mail Address"].ToString())) { continue; }
if (contactList != String.Empty) { contactList += ";"; }
try
{
contactList += dr["Last Name"].ToString() + ", " + dr["First Name"].ToString();
}
catch
{
contactList += dr["Name"].ToString();
}
contactList += "|" + dr["E-mail Address"].ToString();
}
}
return true;
}
catch (Exception ex_process_csv)
{
errorInfo = ex_process_csv.Message;
return false;
}
}
Stream
object - inputStream
- derived from <asp:FileUpload/> .PostedFile.InputStream -
I'm opening Microsoft.VisualBasic.FileIO.TextFieldParser
- new TextFieldParser(inputStream)
.
TextFieldParser
accordingly:
...
Microsoft.VisualBasic.FileIO.TextFieldParser parser =
new TextFieldParser(inputStream);
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
...
...
DataSet contactsData = new DataSet("Contacts");
contactsData.Tables.Add("ContactList");
String[] stringRow = parser.ReadFields();
foreach (String field in stringRow)
{
contactsData.Tables[0].Columns.Add(field, Type.GetType("System.String"));
}
...
ReadFields()
method allows me to reads all fields on the current line, returns them as an array of strings, and advances the cursor to the next line containing data.
TextFieldParser
:
...
while (!parser.EndOfData)
{
stringRow = parser.ReadFields();
contactsData.Tables[0].Rows.Add(stringRow);
}
...