Solved

Visual Basic .net manipluating a csv file

Posted on 2010-11-27
3
570 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

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

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

Suggested Solutions

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
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…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

749 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