如何在vb中读取csv时跳过第一行

时间:2016-06-16 12:19:38

标签: asp.net vb.net

 Try

            If Not MyCache.GetInstance.AzureCacheOn Then
                reader = New IO.StreamReader(FilePathUtility.GetInstance.MapPath(objUrl))
            Else

                request = System.Net.HttpWebRequest.Create(objUrl)
                response = DirectCast(request.GetResponse, System.Net.HttpWebResponse)
                strm = DirectCast(response.GetResponseStream, IO.Stream)
                reader = New IO.StreamReader(strm)
            End If

            Dim list = New List(Of String)()

            Using reader
                Dim line As String
                While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
                    list.Add(line)
                End While
            End Using


            lines = list.ToArray()



        Catch ex As Exception
            Throw ex
        End Try



 Private Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return value
    End Function

我有这个代码片段工作正常但现在在csv文件中我需要在第一行添加一些信息所以我需要跳过第一行我怎么能这样做请帮助附上csv的截图{{3} }

2 个答案:

答案 0 :(得分:3)

在开始处理这些行之前,您只需使用IEnumerable<DataRow>一次:

DataTable

答案 1 :(得分:1)

在while之前调用read行,同时检查该行是否为空,然后只进行循环。 这将跳过你的第一行。

代码类似:

Using reader
    Dim line As String
    // if reader.ReadLine() is not empty
         While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
             list.Add(line)
         End While
    // end if
End Using

要跳过第n行(在您的情况下为第2行),请使用:

     Using reader
        Dim line As String
        // count = 0, n = 1 // 0 = first line, 1 = 2nd line etc
        While (InlineAssignHelper(line, reader.ReadLine())) IsNot Nothing
             // if(count++ != n)                
                 list.Add(line)
             // end if
        End While
    End Using