从vb.net中的csv中排除标题

时间:2018-05-30 11:57:24

标签: vb.net

我有一个.csv,我想将它加载到datagridview中。我有一个名为button1的按钮,我有一个名为datagridview1的datagridview。我点击按钮,它出现......包括标题,我不想要。

请:

How do I exclude the header from the .csv ?

代码:

Imports System.IO
Imports System.Text

Public Class CSV_Reader

    Private Sub CSV_Reader_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim filename As String = "C:\Users\Gaius\Desktop\meepmoop.csv"
        Dim thereader As New StreamReader(filename, Encoding.Default)
        Dim colsexpected As Integer = 7
        Dim sline As String = ""
        DataGridView1.Rows.Clear()

        Do
            sline = thereader.ReadLine
            If sline Is Nothing Then Exit Do
            Dim words() As String = sline.Split(";")
            DataGridView1.Rows.Add("")
            If words.Length = colsexpected Then
                For ix As Integer = 0 To 6
                    DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(ix).Value = words(ix)
                Next
            Else
                DataGridView1.Rows(DataGridView1.Rows.Count - 2).Cells(0).Value = "ERROR"
            End If
        Loop
        thereader.Close()
    End Sub

End Class

meepmoop.csv:

alpha;bravo;charlie;delta;echo;foxtrot;golf
1;meep;moop;meep;moop;meep;moop
2;moop;meep;moop;meep;moop;meep
3;meep;moop;meep;moop;meep;moop
4;moop;meep;moop;meep;moop;meep
5;meep;moop;meep;moop;meep;moop
6;moop;meep;moop;meep;moop;meep
7;meep;moop;meep;moop;meep;moop
8;moop;meep;moop;meep;moop;meep
9;meep;moop;meep;moop;meep;moop
10;moop;meep;moop;meep;moop;meep

编辑:

[...]
Dim sline As String = ""
DataGridView1.Rows.Clear()
Dim line As String = thereader.ReadLine()
If line Is Nothing Then Return

Do
    sline = thereader.ReadLine
[...]

以上对代码的补充有效,但我不明白为什么。我也不明白为什么我要-2而不是-1。我不能依赖猜测,我希望有一天能做到这一点。但我无法绕过它。欢迎解释。

编辑:

Do
    sline = thereader.ReadLine
    If sline Is Nothing Then Exit Do
    Dim words() As String = sline.Split(";")
    If words.Count = 7 Then
        DataGridView1.Rows.Add(words(0), words(1), words(2), words(3), words(4), words(5), words(6))
    Else
        MsgBox("ERROR - There are " & words.Count & " columns in this row and there must be 7!")
    End If
Loop

我根据同事的建议缩短了Loop,并且说“以这种方式更好”。

3 个答案:

答案 0 :(得分:0)

您可以通过拨打ReadLine两次跳过标题。还可以使用Using - 声明:

Using thereader As New StreamReader(filename, Encoding.Default)
    Dim colsexpected As Integer = 7
    Dim sline As String = ""

    Dim line As String = thereader.ReadLine() ' header
    if line is Nothing Then  Return

    Do
        sline = thereader.ReadLine()
        If sline Is Nothing Then Exit Do
        Dim words() As String = sline.Split(";"c)
        ' ... '
    Loop
End Using

答案 1 :(得分:0)

您应该使用为此目的而设计和测试的VB.NET类。它是Microsoft.VisualBasic.FileIO.TextFieldParser,您可以在开始循环解析之前通过调用ReadFields()来跳过标题。

答案 2 :(得分:0)

使用Enumerable.Select() + .Skip()

的另一种方法

正如Ondřej回答中所述,这些操作有一个特定的工具:TextFieldParser
但是,如果没有特殊要求且字符串解析足够简单,可以使用标准工具完成,如 Tim Schmelter 答案所示。

此方法枚举Split()方法返回的字符串数组,并将它们分组,然后以不同的方式使用。作为原始文本来源(如本案例中)或DataSource

Dim FileName As String = "C:\Users\Gaius\Desktop\meepmoop.csv"
Dim Delimiter As Char = ";"c
Dim ColsExpected As Integer = 7
If Not File.Exists(FileName) Then Return

Dim Lines As String() = File.ReadAllLines(FileName, Encoding.Default)
Dim StringColumns As List(Of String()) =
    Lines.Select(Function(line) Split(line, Delimiter, ColsExpected, CompareMethod.Text)).
          Skip(1).ToList()

DataGridView1.Rows.Clear()

'If the DataGridView is empty, add a `[ColsExpected]` number of `Columns`:
DataGridView1.Columns.AddRange(Enumerable.Range(0, ColsExpected).
                      Select(Function(col) New DataGridViewTextBoxColumn()).ToArray())

StringColumns.Select(Function(row) DataGridView1.Rows.Add(row)).ToList()

如果您想要包含和使用标头,因为DataGridView为空(它没有预定义的列),您可以使用.csv文件中的标题行创建控件{{ 1}}:

Columns