【问题标题】:How to limit the number of rows to be read in a CSV file using CSVReader in ASP.NET如何在 ASP.NET 中使用 CSVReader 限制要在 CSV 文件中读取的行数
【发布时间】:2014-05-06 00:45:06
【问题描述】:

我正在使用 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

请帮忙。谢谢

【问题讨论】:

    标签: asp.net csv datatable filereader


    【解决方案1】:

    我想应该有一个方法名“ReadNextRecord()”,所以你的while循环应该是这样的

    While CSVReader.ReadNextRecord()
    

    声明一个int k =0 然后做k++

    一旦 K++ 达到 300,就可以 End While。

    【讨论】:

    • 嗨!我试过了,但遇到了这个错误:“ReadBextRecord() 不是 FileIO.TextFieldParser 的成员”
    • 它不是 ReadBextRecord,它是 ReadNextRecord
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    相关资源
    最近更新 更多