Solved

Visual Basic .net manipluating a csv file

Posted on 2010-11-27
3
561 Views
Last Modified: 2013-11-26
Hi
I need to manipulate a CSV file using visual basic .net, what options do I have and could you provide a sample of the code for connecting to the file or reading into an array?
0
Comment
Question by:sykotex
3 Comments
 
LVL 24

Expert Comment

by:alexey_gusev
Comment Utility
in my view you have at least 2 options:

- read the file line by line and then use String.Split(delim) to get all fields in an array

- add a reference to Excel, create the app object etc and try to read the csv into the worksheet:
http://www.vbdotnetheaven.com/UploadFile/ggaganesh/ExcelSpreadsheet04182005093012AM/ExcelSpreadsheet.aspx
http://www.thescarms.com/dotnet/ExcelObject.aspx
http://www.gemboxsoftware.com/GBSpreadsheet.htm (commercial component)

0
 
LVL 31

Expert Comment

by:James Murrell
Comment Utility
0
 
LVL 33

Accepted Solution

by:
hongjun earned 500 total points
Comment Utility
Preparation
1. Create a file named "schema.ini" in the same directory of your executable and have the contents as follows (note we can specify the delimiter and in this case, I set it as %)

 
[NameList.csv]
Format=Delimited(%)

Open in new window


2. Create the csv file (NameList.csv)

The first row is the column name.

 
UserID%"Username"
1%"user1"
2%"user2"

Open in new window


Select code

 
Sub ReadCsv()

        Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Environment.CurrentDirectory & ";" + "Extended Properties='text;FMT=Delimited(;);HDR=YES'")

        Dim da As New OleDbDataAdapter("select * from [NameList.csv]", con)

        Dim dt As New DataTable

        da.Fill(dt)



        If dt IsNot Nothing AndAlso dt.Rows.Count > 0 Then

            For Each row As DataRow In dt.Rows

                'Read all

                Console.WriteLine(row("UserID").ToString() & ", " & row("Username").ToString())

            Next

        End If

    End Sub

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now