Android Question How to load an excel file from the internet?

trueboss323

Active Member
Licensed User
Longtime User
I'm trying to load the excel file in the panel as read-only. But how do I load it directly from the internet?
 

DonManfred

Expert
Licensed User
Longtime User
Download it, save it to sdcard and then open it
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
Thank you Don , here is what I tried:

B4X:
Sub Globals
    Dim table1 As Table
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.AddMenuItem("Load Table", "LoadTable")

    Dim job1 As HttpJob
    'Send a GET request
    job1.Initialize("Job1", Me)
    job1.Download("https://www.dropbox.com/s/ja1wc5i4uf5pb5r/Book1.xls?raw=1")
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            
            Case "Job1"
                'saves the file to the directory
                Dim out As OutputStream
                out = File.OpenOutput(File.DirDefaultExternal,"Test.xls",False )
                File.Copy2(Job.GetInputStream, out)
                out.Close
            
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub


Sub LoadTable_Click
    LoadTable(File.DirDefaultExternal, "Test.xls")
End Sub

Sub LoadTable(Dir As String, FileName As String)
    Dim workbook1 As ReadableWorkbook
    Dim moviesSheet As ReadableSheet
    workbook1.Initialize(Dir,FileName)
    moviesSheet = workbook1.GetSheet(0)
    If table1.IsInitialized Then
        Activity.RemoveAllViews 'remove the current table
    End If
    table1.Initialize(Me, "Table1", moviesSheet.ColumnsCount)
    table1.AddToActivity(Activity, 0, 0, 100%x, 100%y)
    For row = 0 To moviesSheet.RowsCount - 1
        Dim values(moviesSheet.ColumnsCount) As String
        For i = 0 To values.Length -1
            values(i) = moviesSheet.GetCellValue(i, row)
        Next
        If row = 0 Then
            table1.SetHeader(values)
        Else
            table1.AddRow(values)
        End If
    Next
End Sub

It works when I run it , but please let me know if I did the code correctly.
 
Upvote 0
Top