VB.Net Tutorial/Development/CSV

Материал из VB Эксперт
Перейти к: навигация, поиск

Create CSV file

Public Class Tester
    Public Shared Sub Main
        Dim csvFile As String = My.Application.Info.DirectoryPath & "\Test.csv"
        Dim outFile As IO.StreamWriter = My.ruputer.FileSystem.OpenTextFileWriter(csvFile, False)

        outFile.WriteLine("Column 1, Column 2, Column 3")
        outFile.WriteLine("1.23, 4.56, 7.89")
        outFile.WriteLine("3.21, 6.54, 9.87")
        outFile.Close()
        Console.WriteLine(My.ruputer.FileSystem.ReadAllText(csvFile))

    End Sub
End Class
Column 1, Column 2, Column 3
1.23, 4.56, 7.89
3.21, 6.54, 9.87

Read in and process a CSV file

Public Class Tester
    Public Shared Sub Main
        Dim wholeFile As String
        Dim lineData() As String
        Dim fieldData() As String
        Dim filePath As String = My.ruputer.FileSystem.CurrentDirectory & "\Test.csv"
        wholeFile = My.ruputer.FileSystem.ReadAllText(filePath)
        lineData = Split(wholeFile, vbNewLine)
        For Each lineOfText As String In lineData
            fieldData = lineOfText.Split(",")
            For Each wordOfText As String In fieldData
                Console.WriteLine(wordOfText)
            Next wordOfText
            Console.WriteLine("")
        Next lineOfText
        
    End Sub
End Class
Alpha
Beta
Gamma1.1
 2.3
 4.5

Use system default editor to open a CSV file

Imports System.Diagnostics
Public Class Tester
    Public Shared Sub Main
        Dim csvFile As String = My.Application.Info.DirectoryPath & "\Test.csv"
        Dim outFile As IO.StreamWriter = My.ruputer.FileSystem.OpenTextFileWriter(csvFile, False)

        outFile.WriteLine("Column 1, Column 2, Column 3")
        outFile.WriteLine("1.23, 4.56, 7.89")
        outFile.WriteLine("3.21, 6.54, 9.87")
        outFile.Close()
        Process.Start(csvFile)

    End Sub
End Class