如何使用ASP.NET中的CSVReader限制CSV文件中要读取的行数

时间:2014-05-06 00:45:07

标签: asp.net csv datatable filereader

我正在使用csvReader来从csv文件中检索数据。我的csv文件由500个元素组成,我只需要前300个。如何限制我的csvReader才能返回300个元素?

 Dim PressureTable As DataTable = GetDataTabletFromCSVFile(BackUpDirDminus1)

    Console.WriteLine("Rows count:" + PressureTable.Rows.Count.ToString)
    Console.ReadLine()

    Using CSVReader As New TextFieldParser(BackUpDirDminus1)
        CSVReader.SetDelimiters(New String() {","})
        CSVReader.HasFieldsEnclosedInQuotes = True
        'read column names
        Dim colFields As String() = CSVReader.ReadFields()
        'For Each column As String In colFields

        While Not CSVReader.EndOfData
            Dim fieldData As String() = CSVReader.ReadFields()
            'Making empty value as null
            For i As Integer = 0 To fieldData.Length-1
                If fieldData(i) = "" Then
                    fieldData(i) = Nothing
                End If
            Next
         PressureTable.Rows.Add(fieldData)
        End While
    End Using

请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

我想应该有一个方法名称" ReadNextRecord()",所以你的while循环应该像

While CSVReader.ReadNextRecord()

宣布int k =0 并做k++

一旦K ++达到300,你就可以结束。