?
Solved

Visual Basic .net manipluating a csv file

Posted on 2010-11-27
3
Medium Priority
?
574 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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…
Suggested Courses

764 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