天天看点

vb.net 写数据csv

Public Class Form2

    Public Structure aa

        Dim a As String

        Dim b As Integer

        Dim c As Boolean

    End Structure

    Public sh(222) As aa

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim fileStream As System.IO.FileStream

        Dim streamReader As System.IO.StreamReader

        Dim streamWriter As System.IO.StreamWriter

        Dim strRow As String

        Dim filePath As String = "e:/CsvTest.csv"

        For i As Integer = 0 To 22

            sh(i).a = "zhangxg"

            sh(i).b = i * i

            sh(i).c = (i Mod 2 = 0)

        Next

        Try

            If (System.IO.File.Exists(filePath)) Then

                System.IO.File.Delete(filePath)

            End If

            fileStream = New IO.FileStream(filePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write)

            streamWriter = New IO.StreamWriter(fileStream, System.Text.Encoding.Default)

            For j As Integer = 0 To 22

                strRow = ""

                strRow += sh(j).a

                If j < 23 Then

                    strRow += ","

                End If

                streamWriter.WriteLine(strRow)

            Next

            streamWriter.Close()

            fileStream.Close()

            Return

        Catch ex As Exception

            Return

        End Try

    End Sub

End Class