How can I read and write CSV files using WPF?

I'm using sharpdevelop for a WPF application (I'm new to WPF, I only know VB and not C #, do I need to learn C # to know WPF?).

and I want to read the CSV file first. after that i want to modify and save the file. any suggestions and pointers for this?

+2


source to share


5 answers


WPF is a user interface technology, and I won't associate it with reading a CSV file. Reading a file and displaying it are two different steps. For the first step, you can use KBCsv :

Dim reader As CsvReader = Nothing
Try
    reader = New CsvReader("C:\data.csv")
    reader.ReadHeaderRecord()
    Dim record As DataRecord = reader.ReadDataRecord

    While (Not record Is Nothing)
        System.Console.WriteLine("{0} is {1} years old", record("Name"), record("Age"))
        record = reader.ReadDataRecord
    End While
Finally
    If (Not reader Is Nothing) Then
        reader.Close()
    End If
End Try

      



For the second step, WPF is an option. But it's also Windows Forms or just a text-out console. Assuming WPF, you can display data internally DataGrid

.

+4


source


There is a lot of code on the internet to read / write CSV files. Here is the complete (and free) library: http://www.filehelpers.com/



Here is a good implementation for CSV parsing if you want to get into the source code for this type of task: http://www.codeproject.com/KB/database/CsvReader.aspx

+2


source


No C # required. WPF can be done in VB.NET too.

You can get fancy and use link text Or VB has TextFieldParser just for this http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.aspx or link link text

 Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser _
("c:\logs\bigfile")
    MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited
    MyReader.Delimiters = New String() {vbTab}
    Dim currentRow As String()
    'Loop through all of the fields in the file. 
    'If any lines are corrupt, report an error and continue parsing. 
    While Not MyReader.EndOfData
        Try
            currentRow = MyReader.ReadFields()
            ' Include code here to handle the row.
        Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
            MsgBox("Line " & ex.Message & _
            " is invalid.  Skipping")
        End Try
    End While
End Using

      

+1


source


To answer the first part of your question. Windows Presentation Foundation is a collection of classes related to the presentation of user interfaces for applications. The classes that make up WPF can be used from any language including VB.NET, so learning C # is not necessary to use WPF

Regarding the CSV component of your question, since WPF is a presentation framework, it doesn't provide the ability to parse the CSV format directly, however CSV is a simple and common format, so writing a parser or finding an existing one shouldn't be too difficult.

While there is no formal specification for the format, Wikipedia does provide a lot of information on this if you decide to write a parser for yourself.

As stated above there are many pre-existing libraries, some of which are open-source icluding this one I found in the code project

NB: I have not used the CSV library mentioned above, it is provided as a starting point.

0


source







All Articles