Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Visual Basic .net manipluating a csv file

Posted on 2010-11-27
3
Medium Priority
?
575 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

604 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