CSV to DataTabe C# all strings
this sln is for when u know / take all the columns as strings, but its very fast and simple.
thx to Alan here for the base:
http://www.c-sharpcorner.com/Forums/Thread/47618/C-Sharp-importing-csv-files-into-datatables.aspx
public static DataTable TransferCSVToTable(string filePath)
{
DataTable dt = new DataTable();
string[] csvRows = System.IO.File.ReadAllLines(filePath);
string[] fields = null;
bool first = true;
foreach (string csvRow in csvRows)
{
fields = csvRow.Split(',');
if (first)
{
foreach (string header in fields)
dt.Columns.Add(new DataColumn(header));
first = false;
}
else
{
DataRow row = dt.NewRow();
row.ItemArray = fields;
dt.Rows.Add(row);
}
}
return dt;
}
thx to Alan here for the base:
http://www.c-sharpcorner.com/Forums/Thread/47618/C-Sharp-importing-csv-files-into-datatables.aspx
public static DataTable TransferCSVToTable(string filePath)
{
DataTable dt = new DataTable();
string[] csvRows = System.IO.File.ReadAllLines(filePath);
string[] fields = null;
bool first = true;
foreach (string csvRow in csvRows)
{
fields = csvRow.Split(',');
if (first)
{
foreach (string header in fields)
dt.Columns.Add(new DataColumn(header));
first = false;
}
else
{
DataRow row = dt.NewRow();
row.ItemArray = fields;
dt.Rows.Add(row);
}
}
return dt;
}
Comments
Post a Comment