Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to read specific CSV cell file? Im using CSVReader to get all data from CSV for sending to database purposes.

Currently there is some csv file that start from some line. Refer image here[^] for sample of this kind of csv.

I want to get data for cell A:4, A:5, A:6 and read all value start from A8:D8 downwards.

I'm only done for reading everything if I remove Row 1-7

VB.NET
Private Sub ReadCSVData(ByVal filepathname As String)
    Using csv As New CsvReader(New StreamReader(filepathname), True)
        Dim fieldCount As Integer = csv.FieldCount
        Dim headers() As String = csv.GetFieldHeaders()
        Dim jsonData As New JArray()
        Dim dt As New DataTable
        Do While csv.ReadNextRecord()
            Dim record As New JObject()
            For i As Integer = 0 To fieldCount - 1
                record.Add(New JProperty(headers(i), csv(i)))
            Next
        Loop
    End Using
End Sub

Now the real csv file is like attach link's image

What I have tried:

Private Sub ReadCSVData(ByVal filepathname As String)
Using csv As New CsvReader(New StreamReader(filepathname), True)
Dim fieldCount As Integer = csv.FieldCount
Dim headers() As String = csv.GetFieldHeaders()
Dim jsonData As New JArray()
Dim dt As New DataTable
Do While csv.ReadNextRecord()
Dim record As New JObject()
For i As Integer = 0 To fieldCount - 1
record.Add(New JProperty(headers(i), csv(i)))
Next
Loop
End Using
End Sub
Posted
Updated 25-Aug-16 3:16am
Comments
Richard MacCutchan 25-Aug-16 3:57am    
You just need to use a counter to read the first 7 lines but ignore them. So you only process the cells when your line count reaches 8.

1 solution

Assuming you're always skipping the first three lines, and the CSV data is always separated from the extra data by a blank line, something like this should work:
VB.NET
Private Function ReadCSVData(ByVal filepathname As String) As JObject
    Dim result As New JObject()
    
    Using sr As New StreamReader(filepathname)
        ' Skip the first three lines:
        sr.ReadLine()
        sr.ReadLine()
        sr.ReadLine()
        
        ' Grab the extra data from the next lines:
        Dim line As String = sr.ReadLine()
        Do Until String.IsNullOrWhiteSpace(line)
            Dim parts() As String = line.Split(":"c)
            If parts.Length = 2 Then
                result.Add(New JProperty(parts(0).Trim(), parts(1).Trim()))
            End If
            
            line = sr.ReadLine()
        Loop
        
        ' Read the CSV data:
        Using csv As New CsvReader(sr, True)
            Dim fieldCount As Integer = csv.FieldCount
            Dim headers() As String = csv.GetFieldHeaders()
            Dim jsonData As New JArray()
            
            Do While csv.ReadNextRecord()
                Dim record As New JObject()
                For i As Integer = 0 To fieldCount - 1
                    record.Add(New JProperty(headers(i), csv(i)))
                Next
                
                jsonData.Add(record)
            Loop
            
            result.Add(New JProperty("data", jsonData))
        End Using
    End Using
    
    Return result
End Function

Output:
JavaScript
{
  "Sensor ID": "...",
  "Sensor Description": "...",
  "Status": "...",
  "data": [
    {
      "Time": "...",
      "Sensor A": "...",
      "Sensor B": "...",
      "Sensor C": "..."
    },
    ...
  ]
}
 
Share this answer
 
Comments
Luiey Ichigo 26-Aug-16 23:41pm    
Again, thanks for your guide! You idea is very helping

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900