Importing a CSV file

B

BobLaughland

What is the best way to import the contents of a CSV file into C#
objects?

E.G. a CSV file that looks like this

a1,b1,c1,d1,
a2,b2,c2,d2,
a3,b3,c3,d3

Would be loaded up and then turned into 3 objects

object 1
field1 = a1
field2 = b1
field3 = c1
field4 = d1

object 2
field1 = a2
field2 = b2
field3 = c2
field4 = d2

object 3
field1 = a3
field2 = b3
field3 = c3
field4 = d3

Thanks,

Peter.
 
G

Guest

What is the best way to import the contents of a CSV file into C#
objects?


Depending on how large the text file is... I would use Microsoft's Text
Driver. MS Text Driver has all the parsing code built in, and you can do a
"SELECT * " on the file.

Otherwise, you can open an IO stream and read the file line by line.

If the file is really large, you might want to load the data into a
database first - using DTS. You can create dynamic DTS graphs in .NET to
handle unknown file structures.
 
G

Guest

I would use IO.StreamReader class to open the file, then read it into a
dataTable.
Here is the function that does just that in VB.Net (but it can be easily
converted to C# to suit your needs though :)

To use it, you do something like
Dim dt As DataTable = BuildDataTable("C:\myTestFile.txt", ",") 'for CSV file
Once you get this data table, you can do whatever you want with it... Each
datarow serves as one object in your question.

'Reading file to datatable
Private Function BuildDataTable(ByVal fileFullPath As String, ByVal
seperator As Char) As DataTable

Dim myTable As DataTable = New DataTable("MyTable")
Dim i As Integer
Dim myRow As DataRow
Dim fieldValues As String()
Dim f As IO.File
Dim myReader As IO.StreamReader
Try
'Open file and read first line to determine how many fields
there are.
myReader = f.OpenText(fileFullPath)
fieldValues = myReader.ReadLine().Split(seperator)
'Create data columns accordingly
For i = 0 To fieldValues.Length() - 1
myTable.Columns.Add(New DataColumn("Field" & i))
Next
'Adding the first line of data to data table
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
'Now reading the rest of the data to data table
While myReader.Peek() <> -1
fieldValues = myReader.ReadLine().Split(seperator)
myRow = myTable.NewRow
For i = 0 To fieldValues.Length() - 1
myRow.Item(i) = fieldValues(i).ToString
Next
myTable.Rows.Add(myRow)
End While
Catch ex As Exception
MsgBox("Error building datatable: " & ex.Message)
Return New DataTable("Empty")
Finally
myReader.Close()
End Try

Return myTable
End Function

Hope this helps.
VHD50.
 
Joined
Nov 11, 2009
Messages
1
Reaction score
0
It is easy to import csv to datagridview

Use the following function.

{
DataTable datatbl = ImportCSV(strFleName, delimeter, false);
datagridview.DataSource = datatbl;
}

I just read and implemented it it is working u can download full source from
tctcworld.com/dotnet/viewtopic.php?f=1&t=463

Happy programing
:ciao:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,811
Latest member
SaulFernan

Latest Threads

Top