VB.Net:在Datagridview中打开CSV文件

时间:2018-09-12 11:08:43

标签: vb.net csv datagridview streamreader

我无法将数据从CSV文件加载到Datagridview。 我在尝试读取文件时遇到以下错误: “输入数组长于此表中的列数”

Dim SR As StreamReader = New StreamReader(My.Computer.FileSystem.SpecialDirectories.Programs & "/PURTB/Data/" & TextBox1.Text & ComboBox3.Text & ".csv")
    Dim line As String = SR.ReadLine()
    Dim strArray As String() = line.Split(","c)
    Dim dt As DataTable = New DataTable()
    Dim row As DataRow

    For Each s As String In strArray
        dt.Columns.Add(New DataColumn())
    Next

    Do
        line = SR.ReadLine
        If Not line = String.Empty Then
            row = dt.NewRow()
            row.ItemArray = line.Split(",")
            dt.Rows.Add(row)
        Else
            Exit Do
        End If
    Loop

我正在使用的文件如下所示: CSV File

更新:也许问题出在出口代码上。

Dim StrExport As String = ""
    For Each C As DataGridViewColumn In DataGridView1.Columns
        StrExport &= """" & C.HeaderText & ""","
    Next
    StrExport = StrExport.Substring(0, StrExport.Length - 1)
    StrExport &= Environment.NewLine

    For Each R As DataGridViewRow In DataGridView1.Rows
        For Each C As DataGridViewCell In R.Cells
            If Not C.Value Is Nothing Then
                StrExport &= """" & C.Value.ToString & ""","
            Else
                StrExport &= """" & "" & ""","
            End If
        Next
        StrExport = StrExport.Substring(0, StrExport.Length - 1)
        StrExport &= Environment.NewLine
    Next

    Dim tw As IO.TextWriter = New IO.StreamWriter(My.Computer.FileSystem.SpecialDirectories.Programs & "/PURTB/Data/" & TextBox1.Text & ComboBox3.Text & ".csv")
    tw.Write(StrExport)
    tw.Close()

2 个答案:

答案 0 :(得分:0)

一旦我需要在DataGridView中导入CSV文件,并且按照下面的代码进行操作(许多用户仍在使用它,并且没有错误):

Dim dt As DataTable = New DataTable 'you will not need this if your dgv has a datatable as datasource
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fName As String = ""
    OpenFileDialog1.InitialDirectory = "D:\TestFile"
    OpenFileDialog1.Filter = "CSV files(*.csv)|*.csv"
    OpenFileDialog1.RestoreDirectory = True
    If (OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK) Then
        fName = OpenFileDialog1.FileName
    End If

    Dim TextLine As String = ""
    Dim SplitLine() As String

    If System.IO.File.Exists(fName) = True Then
        Dim objReader As New System.IO.StreamReader(fName, Encoding.ASCII)
        Dim index As Integer = 0
        Do While objReader.Peek() <> -1
            If index > 0 Then
                TextLine = objReader.ReadLine()
                SplitLine = Split(TextLine, ",")
                dt.Rows.Add(SplitLine)
            Else
                TextLine = objReader.ReadLine()
            End If
            index = index + 1
        Loop
        DataGridView1.DataSource = dt
    Else
        MsgBox("File Does Not Exist")
    End If
End Sub

希望它能起作用。 ^^

答案 1 :(得分:0)

使用数据表时,您需要定义列...否则,将得到OP指定的错误:“ 输入数组长于该表中的列数” -如果添加以下代码,则上面的代码将起作用...

    dt.Columns.Add("Column1", GetType(String))
    dt.Columns.Add("Column2", GetType(String))
    dt.Columns.Add("Column3", GetType(String))
    dt.Columns.Add("Column4", GetType(String))
    dt.Columns.Add("Column5", GetType(String))

    etc.