Android Question problem reading csv file with Excel mobile

marcick

Well-Known Member
Licensed User
Longtime User
Hi all,
i'm creating a csv file and allowing the user to save it where he wants in the device. Code:

B4X:
Dim csvlist as list
Dim FileHandler1 As FileHandler

csvlist.initialize
Dim rw(3)As String
rw(0)="value1"
rw(1)="value2"
rw(2)="value3"
csvlist.Add(rw)
rw(0)="value1"
rw(1)="value2"
rw(2)="value3"
csvlist.Add(rw)
Dim CSVHeaders As List
CSVHeaders.Initialize2(Array As String("Field1","Field2","Field3")
Dim su As StringUtils
su.SaveCSV2(File.DirInternal,"mycsv.csv",",",csvlist,CSVHeaders)
FileHandler1.Initialize
Wait For (FileHandler1.SaveAs(File.OpenInput(File.DirInternal,"mycsv.csv"), "text/csv", "mycsv.csv")) Complete (Success As Boolean)

Now, if I transfer the generated file on a Windows PC and open it with Excel, it looks correct, with the header and separate columns.
But if I open it directly on the device with Excel Mobile, the file is opened as csv text and not formatted in columns

B4X:
Field1,Field2,Field3
Value1,Value2,Value3
Value1,Value2,Value3

Where is the problem ?

On B4i side instead, the file is generated with the same identical code and then saved with this other code and then it is correctly opened on the device with Excel Mobile.

B4X:
Dim avc As ActivityViewController
avc.Initialize("avc",  Array(MySub.CreateFileUrl(File.DirTemp,"mycsv.csv")))
avc.Show(Page1, Page1.RootPanel)
Wait For avc_Complete (Success As Boolean, ActivityType As String)
 

Spavlyuk

Active Member
Licensed User
Are the devices using the same culture/language? It's possible for a culture to have a different comma separator, such as ';', which could cause issues when opening csv files.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Compare the content of the two files with Notepad++ or a similar text editor.

They are apparently identical
I tried to change the encoding to "Windows-1252" but same result

B4X:
csvlist.initialize
Dim rw(3)As String
rw(0)="value1"
rw(1)="value2"
rw(2)="value3"
csvlist.Add(rw)
rw(0)="value1"
rw(1)="value2"
rw(2)="value3"
csvlist.Add(rw)
Dim CSVHeaders As List
CSVHeaders.Initialize2(Array As String("Field1","Field2","Field3")
Dim su As StringUtils
su.SaveCSV2(File.DirInternal,"mycsv.csv",",",csvlist,CSVHeaders)

    Dim tr As TextReader
    tr.Initialize(File.OpenInput(File.DirInternal,"mycsv.csv"))
    Dim trs As String=tr.ReadAll
    Dim tro As TextWriter
    tro.Initialize2(File.OpenOutput(File.DirInternal,"mycsv2.csv",False),"Windows-1252")
    tro.Write(trs)
    tro.Close

FileHandler1.Initialize
Wait For (FileHandler1.SaveAs(File.OpenInput(File.DirInternal,"mycsv2.csv"), "text/csv", "mycsv2.csv")) Complete (Success As Boolean)

I discovered also that the file produced in IOS i correctly opened in Excel mobile for IOS (and Excel for Windows), but not in Excel Mobile for Android.
Perhaps the Android version of Excel does not format properly csv files with headers ?
 
Upvote 0

groena

New Member
Licensed User
Longtime User
Did you try with adding “Sep=,” at the first line of the file?
File.WriteString(File_Dir,File_Name,"Sep=,")
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
I'm working on another project now but that problem is still pending. I'll check it in the next days and report you back. Thanks for your reply.
 
Upvote 0
Top