B4A Library xCSVHelper - wrapper from Apache Commons CSV

Apache Commons is a powerful library for handling CSV files. It offers features like reading arbitrary numbers of values per line and ignoring commas within quoted elements.

Sources Code: Github

Examples:
#AdditionalJar: commons-csv-1.10.0.jar
#MultiDex: true

Sub Process_Globals  
    Private xui As XUI
   
    Dim csvparsering As xCSVHelper
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    File.Copy(File.DirAssets,"customers-100.csv", File.DirInternalCache, "file1.csv")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   
   
    Dim filepath As String = File.Combine(File.DirInternalCache, "file1.csv")
    'Index,Customer Id,First Name,Last Name,Company,City,Country,Phone 1,Phone 2,Email,Subscription Date,Website
    Dim fileds() As String = Array As String("_id","_customerid","_firstname")
    csvparsering.FieldSeparator = ","
    csvparsering.SkipEmptyRows = True
    csvparsering.ErrorOnDifferentFieldCount = True
    Dim lstrs As List = csvparsering.Parser(filepath, fileds)
    Log("Size=" & lstrs.Size)
   
    For i=0 To lstrs.Size - 1
        Dim row As Map = lstrs.Get(i)
        Log(row.Get("_firstname"))
    Next
End Sub

Parse csv files directly:
Private Sub Button2_Click
    Dim filepath As String = File.Combine(File.DirInternalCache, "file1.csv")
    csvparsering.Initialize("csvparsering")
    csvparsering.FieldSeparator = ","
    csvparsering.SkipEmptyRows = True
    csvparsering.ErrorOnDifferentFieldCount = True   
    csvparsering.Parser2(filepath)
End Sub

Sub csvparsering_Parsing(row As xCSVRecord)
    
    Log("col=" &  row.getByColumn(0))
    Log("col=" &  row.getByColumn(1))
    Log("col=" &  row.getByColumn(2))
End Sub
 

Attachments

  • xCSVHelper.zip
    2.4 KB · Views: 107
  • commons-csv-1.10.0.jar
    53.5 KB · Views: 109
  • xCSVHelper_1.22.zip
    4.8 KB · Views: 81
  • B4A Test.zip
    93.4 KB · Views: 84
Last edited:

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Apache Commons is a powerful library for handling CSV files. It offers features like reading arbitrary numbers of values per line and ignoring commas within quoted elements.

Sources Code: Github

Examples:
#AdditionalJar: commons-csv-1.10.0.jar
#MultiDex: true

Sub Process_Globals  
    Private xui As XUI
   
    Dim csvparsering As xCSVHelper
End Sub

Sub Globals
   
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    File.Copy(File.DirAssets,"customers-100.csv", File.DirInternalCache, "file1.csv")
   
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
   
   
    Dim filepath As String = File.Combine(File.DirInternalCache, "file1.csv")
    'Index,Customer Id,First Name,Last Name,Company,City,Country,Phone 1,Phone 2,Email,Subscription Date,Website
    Dim fileds() As String = Array As String("_id","_customerid","_firstname")
    csvparsering.FieldSeparator = ","
    csvparsering.SkipEmptyRows = True
    csvparsering.ErrorOnDifferentFieldCount = True
    Dim lstrs As List = csvparsering.Parser(filepath, fileds)
    Log("Size=" & lstrs.Size)
   
    For i=0 To lstrs.Size - 1
        Dim row As Map = lstrs.Get(i)
        Log(row.Get("_firstname"))
    Next
End Sub
Would it be possible to make the output different, so that the list will hold string arrays instead of a single strings repeating the field names for
every row?
It would make it a bit faster and also a bit easier to process the list, eg moving data to a DB.

One other thing to mention:

1. the provided example code doesn't show it, but there will be an error if the charset is not specified, so add something like:
B4X:
csvparsering.Charset = "UTF-8"

RBS
 

tummosoft

Member
Licensed User
Longtime User
Would it be possible to make the output different, so that the list will hold string arrays instead of a single strings repeating the field names for
every row?
It would make it a bit faster and also a bit easier to process the list, eg moving data to a DB.

One other thing to mention:

1. the provided example code doesn't show it, but there will be an error if the charset is not specified, so add something like:
B4X:
csvparsering.Charset = "UTF-8"

RBS
Thank you!
The new version has just been updated! Now you can parse csv files directly without passing in collections.

B4X:
Private Sub Button2_Click
    Dim filepath As String = File.Combine(File.DirInternalCache, "file1.csv")
    csvparsering.Initialize("csvparsering")
    csvparsering.FieldSeparator = ","
    csvparsering.SkipEmptyRows = True
    csvparsering.ErrorOnDifferentFieldCount = True   
    csvparsering.Parser2(filepath)
End Sub

Sub csvparsering_Parsing(row As xCSVRecord)
    
    Log("col=" &  row.getByColumn(0))
    Log("col=" &  row.getByColumn(1))
    Log("col=" &  row.getByColumn(2))
End Sub
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Thank you!
The new version has just been updated! Now you can parse csv files directly without passing in collections.

B4X:
Private Sub Button2_Click
    Dim filepath As String = File.Combine(File.DirInternalCache, "file1.csv")
    csvparsering.Initialize("csvparsering")
    csvparsering.FieldSeparator = ","
    csvparsering.SkipEmptyRows = True
    csvparsering.ErrorOnDifferentFieldCount = True  
    csvparsering.Parser2(filepath)
End Sub

Sub csvparsering_Parsing(row As xCSVRecord)
   
    Log("col=" &  row.getByColumn(0))
    Log("col=" &  row.getByColumn(1))
    Log("col=" &  row.getByColumn(2))
End Sub
Thanks, will have a look.

RBS
 
Top