iOS Question Excel conversion throught a server

marcick

Well-Known Member
Licensed User
Longtime User
Hi all,
I need my App be able to generate an Excel files. I do it in B4A but I know there is not the Excel library in B4i.
Because my app communicate with my server where a B4J server application is running, I was thinking to send a text file or a db to the server, that make the conversion in B4J and send back the Excel file.
Which is the best way to excange files (let's say small files less than 500KB) from B4A and the server ?
Thanks
 

emexes

Expert
Licensed User
What happens with your generated Excel files? How are they handled/used after they leave your app? Does the process already exist, eg are Excel files like those that your app needs to generate, already being created by an existing program, or manually (presumably in Excel) ?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Once the Excel file is generated, the use can open it with office mobile or eventually share and send it to an email address etc.
But this is not really relevant.
Which is the best way to excange files from B4A and the B4J server ?
 
Upvote 0

emexes

Expert
Licensed User
But this is not really relevant.

I would say it is super-relevant. If your app can directly produce a file that serves whatever purpose the Excel file is being used for, then you'd no longer need to run a server and transfer files back and forth. Less complexity, less cost, less to go wrong, fewer links to break. But I am getting the vibe that there are other considerations at play here too, so... no worries, I'll not push that barrow any further. ?

My first go-to for transferring files between computers with non-permanent IP addresses or domain names, is to use FTP, especially if your company already has an FTP server operating somewhere. Bonus is that you (can) have a log/archive/backup of the data transferred, if the intermediate files have unique names eg based on date-time-user. Downside is that the latency/turnaround-time might drive your users nuts.
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
If your app can directly produce a file that serves whatever purpose the Excel file is being used for ......

No, my app can't.
My B4A app can, but B4i app not, because there is no Excel library for B4i. There is a workaround that I'm using but I want to move to another solution.
And I need a standard Excel file.

I don't want to install the FTP service on my WAMP server, I had trouble in the past.
Because files are small size, I would prefer some other method (HTTP request ? Websocket ?)
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Simply send a post request with the file contents and read it on the server with:

B4X:
Dim b() As Byte = Bit.InputStreamToBytes(req.InputStream)

Thank you !.
I found the full Tutorial to send files.
Now, if the file is a text file and I just need to scan all the lines, how do I manage the b() array ? Sorry if the question looks stupid ....
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
trying to manage everything more efficient, I need to use the j.postbytes command to transmit a list (containing strings)
how to convert the list to a byte array ?

I'm using this code but get an error

B4X:
Sub ListToBytes(list As List) As Byte()
    Dim b(list.Size) As Byte
    For i = 0 To list.Size - 1
        b(i) = list.Get(i)
    Next
    Return b
End Sub
 
Upvote 0

emexes

Expert
Licensed User
I need to use the j.postbytes command to transmit a list (containing strings)
how to convert the list to a byte array ?

Use ByteBuilder. I'd probably start the byte array off with 4 byte Int of the number of strings in the list, and then for each string in the list, add 4 byte int of the string's length followed by the string bytes.

Uh, just realised that the string length in bytes could be different to the string length in characters. No problem, though: convert the string to bytes first in a temporary array, then add the length of that temporary array, and then the temporary array, to the final array.

It wouldn't surprise me if Serializer did the same thing, but with extra tags to indicate the types List and String.
 
Last edited:
Upvote 0

emexes

Expert
Licensed User
then for each string in the list, add 4 byte int of the string's length followed by the string bytes.

Or if your strings never contain a newline character aka CRLF aka Chr(10), then forget the lengths, just add a terminator to each string (except maybe the last one, although it'd be "cleaner" if you did) and concatenate them all together. At the other end, to reverse the process, convert the bytes back into a string, then Regex.Split, which handily will should also trim the trailing empty unterminated line.

The nice thing about doing it this way, rather than Serialization, is that the intermediate bytes, if they are in a file, are readable with pretty much any text editor (assuming you go with UTF-8 encoding).
 
Upvote 0

emexes

Expert
Licensed User
I am sure a function like this already exists somewhere, but %@#ed if I can find it:

B4X:
Sub RegexUnsplitStringList(Separator As String, SL As List) As String

    Dim sb As StringBuilder
    For I = 0 the SL.Size - 1
        sb.Add(SL.Get(I))
        sb.Add(Separator)
    Next
   
    Return sb.ToString

End Sub
B4X:
Sub RegexUnsplitStringArray(Separator As String, SA() As String) As String

    Dim sb As StringBuilder
    For I = 0 the SA.Length - 1
        sb.Add(SL(I))
        sb.Add(Separator)
    Next
   
    Return sb.ToString
   
End Sub
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…