Android Question Export to CSV can't be read.

makis_best

Active Member
Licensed User
I export some info from sqlite database to csv file.
The data in database are in Greek.

The code I use to save data is
B4X:
Public Sub SaveTableToCSV(Dir As String, Filename As String)
    Dim headers(mNumberOfColumns) As String
    For i = 0 To headers.Length - 1
        Dim L As Label
        L = Header.GetView(i)
        headers(i) = L.Text
    Next
    StringUtils1.SaveCSV2(Dir, Filename, ",", Data, headers)
End Sub
But when I open the CSV file I cant read anything and the excel show something like.

Ξ—ΞΌΞ½Ξ―Ξ±,Παραστατικό,Αιτιολογία,ΧρΞωση,Πίστωση,Υπόλοιπο
,,Εκ μεταφοράς (Ξως 30/06/2019),0.00 €,0.00 €,782.22 €
,ΑΕΠ-Χ-03664,Απόδειξη είσπραξης ΑΠΔ Δ Νο 9886,0.00 €,758.75 €,-758.75 €
15/07/2019,ΤΔΑ-11537,Τιμολόγιο Πώλησης - Δελτίο αποστολής ,368.99 €,0.00 €,-389.76 €
07/08/2019,ΤΔΑ-11956,Τιμολόγιο Πώλησης - Δελτίο αποστολής ,289.35 €,0.00 €,-100.41 €
12/08/2019,ΑΕΠ-Χ-03800,Απόδειξη είσπραξης ΑΠΔ Ζ Νο 9419,0.00 €,658.34 €,-758.75 €
24/09/2019,ΤΔΑ-12613,Τιμολόγιο Πώλησης - Δελτίο αποστολής ,409.66 €,0.00 €,-349.09 €
,,Σύνολα,1068.00 €,1417.09 €,-349.09 €
How I can fix it?
 

RB Smissaert

Well-Known Member
Licensed User
I export some info from sqlite database to csv file.
The data in database are in Greek.

The code I use to save data is
B4X:
Public Sub SaveTableToCSV(Dir As String, Filename As String)
    Dim headers(mNumberOfColumns) As String
    For i = 0 To headers.Length - 1
        Dim L As Label
        L = Header.GetView(i)
        headers(i) = L.Text
    Next
    StringUtils1.SaveCSV2(Dir, Filename, ",", Data, headers)
End Sub
But when I open the CSV file I cant read anything and the excel show something like.



How I can fix it?
You don't show how you got your data and I think headers needs to be a list, not an array.

RBS
 

makis_best

Active Member
Licensed User
I read data from a table

B4X:
Sub Globals
    Private Kartela_Table As Table
End Sub

Sub SendBtn_Click
    Dim Message As Email
    Message.To.Add(EmailEditText.Text)
    Message.Subject = "ΚΑΡΤΕΛΑ ΠΕΛΑΤΗ: " & NameEditText.Text
    Kartela_Table.SaveTableToCSV(Starter.SafeFolder, "kartela - " & NameEditText.Text & ".csv")
    Message.Attachments.Add(File.Combine(Starter.SafeFolder, "kartela - " & NameEditText.Text & ".csv"))
    StartActivity(Message.GetIntent)
End Sub
 

DonManfred

Expert
Licensed User

klaus

Expert
Licensed User
I'm afraid that it's an encoding problem.
SaveTableToCSV encodes in UTF-8.
Excel expects "windows 125x" encoding.
If you have Notepad++ you can check the encoding.
 
Last edited:

klaus

Expert
Licensed User
The problem is Excel not B4A.
If you want to read it with Excel you need to change the encoding.
As a workaround, after having saved the file with SaveTableToCSV, you can:
Read the file back with TextReader.ReadAll and save it back with TextWriter.Initialize2 and TextWriter.Write where you can set the encoding you need.
 

makis_best

Active Member
Licensed User
OK... I make the chances with TextWriter.Initialize2 but now StartActivity(Message.GetIntent)
not working properly... It doesn't show the list of available applications to choose from but
start the Exchange server settings
 
Top