Solved

Visual Basic .net manipluating a csv file

Posted on 2010-11-27
3
566 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
ID: 34224971
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
ID: 34224985
0
 
LVL 33

Accepted Solution

by:
hongjun earned 500 total points
ID: 34225028
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

932 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

12 Experts available now in Live!

Get 1:1 Help Now