• We just launched and are currently in beta. Join us as we build and grow the community.

Android Excel File Reader Application using Basic4Android - Tutorial Part 1

oatzaz

Reverse Shell Engineer
O Rep
0
0
0
Rep
0
O Vouches
0
0
0
Vouches
0
Posts
99
Likes
44
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
Today, i will introduce a powered application in Android that views an Excel File. Some of the Android Phones today is not reliable to open and read an Excel File in their Android Phones. But, reading this tutorial makes you realize that you can program the Android to open an Excel File.

The first thing that you will consider is to copy an Excel File to the File Folder of your Basic4Android Application. You need to create an excel file like this one below and named it "book1.xls".

excelfile.jpg

  1. Sub

    Globals
  2. Dim

    table1 As

    Table
  3. End

    Sub

- The code above is the initialization of reading the table in an Excel file. The table1 here is the variable in my table. Note: Don't Forget to add the Library of Excel in the Library of Basic4Android.

  1. Sub

    Activity_Create(FirstTime As

    Boolean

    )
  2. Activity.Title = "Excel Example - Lyndon Bermoy"
  3. Activity.AddMenuItem("Load Table"

    , "LoadTable"

    )
  4. Activity.AddMenuItem("Save Table"

    , "SaveTable"

    )
  5. LoadTable(File.DirAssets, "Book1.xls"

    )
  6. End

    Sub

- Given the code above is the same with Form_Load in Visual Basic. In our activity it have two Menu Item, the load table and saving table.
The LoadTable(File.DirAssets, "Book1.xls"

)

syntax is getting the information of our table created in Excel.

Next, we should have a procedure of clicking our load table in the excel. Write this code:

  1. Sub

    LoadTable_Click
  2. If

    File.Exists(File.DirRootExternal, "book1.xls"

    ) = False

    Then
  3. ToastMessageShow("Unable to open file."

    ,True

    )
  4. Else
  5. LoadTable(File.DirRootExternal, "book1.xls"

    )
  6. End

    If
  7. End

    Sub

- I the code above had not found the book1.xls then it will prompt the user "Unable to open file". Otherwise, it will load the table of book1.xls.

Here is the partial code in our Main Program in Basic4Android:

  1. Sub

    Process_Globals
  2. End

    Sub

  3. Sub

    Globals
  4. Dim

    table1 As

    Table
  5. End

    Sub

  6. Sub

    Activity_Create(FirstTime As

    Boolean

    )
  7. Activity.Title = "Excel Example - Lyndon Bermoy"
  8. Activity.AddMenuItem("Load Table"

    , "LoadTable"

    )
  9. Activity.AddMenuItem("Save Table"

    , "SaveTable"

    )
  10. LoadTable(File.DirAssets, "Book1.xls"

    )
  11. End

    Sub

  12. Sub

    LoadTable_Click
  13. If

    File.Exists(File.DirRootExternal, "book1.xls"

    ) = False

    Then
  14. ToastMessageShow("Unable to open file."

    , True

    )
  15. Else
  16. LoadTable(File.DirRootExternal, "book1.xls"

    )
  17. End

    If
  18. End

    Sub

  19. Sub

    LoadTable(Dir As

    String

    , FileName As

    String

    )
  20. Dim

    workbook1 As

    ReadableWorkbook
  21. Dim

    moviesSheet As

    ReadableSheet
  22. workbook1.Initialize(Dir, FileName)
  23. moviesSheet = workbook1.GetSheet(0)
  24. If

    table1.IsInitialized Then
  25. Activity.RemoveAllViews 'remove the current table
  26. End

    If
  27. table1.Initialize(Me, "Table1"

    , moviesSheet.ColumnsCount)
  28. table1.AddToActivity(Activity, 0, 0, 100%x, 100%y)
  29. For

    row = 0 To

    moviesSheet.RowsCount - 1
  30. Dim

    values(moviesSheet.ColumnsCount) As

    String
  31. For

    i = 0 To

    values.Length -1
  32. values(i) = moviesSheet.GetCellValue(i, row)
  33. Next
  34. If

    row = 0 Then
  35. table1.SetHeader(values)
  36. Else
  37. table1.AddRow(values)
  38. End

    If
  39. Next
  40. End

    Sub

  41. Sub

    SaveTable_Click
  42. 'first we create a writable workbook.
  43. 'the target File should be a NEW File.
  44. Dim

    newWorkbook As

    WritableWorkbook
  45. newWorkbook.Initialize(File.DirRootExternal, "1.xls"

    )
  46. Dim

    sheet1 As

    WritableSheet
  47. sheet1 = newWorkbook.AddSheet("Movies"

    , 0)
  48. 'add the headers To the sheet
  49. 'we create a special format For the headers
  50. Dim

    cellFormat As

    WritableCellFormat

  51. cellFormat.Initialize2(cellFormat.FONT_ARIAL, 12, True

    , False

    , False

    , _
  52. cellFormat.COLOR_GREEN)
  53. cellFormat.HorizontalAlignment = cellFormat.HALIGN_CENTRE
  54. cellFormat.SetBorder(cellFormat.BORDER_ALL, _
  55. cellFormat.BORDER_STYLE_MEDIUM, cellFormat.COLOR_BLACK)
  56. cellFormat.SetBorder(cellFormat.BORDER_BOTTOM, cellFormat.BORDER_STYLE_THICK, _
  57. cellFormat.COLOR_BLUE)
  58. cellFormat.VertivalAlignment = cellFormat.VALIGN_CENTRE
  59. cellFormat.BackgroundColor = cellFormat.COLOR_GREY_25_PERCENT
  60. Dim

    col As

    Int = 0
  61. For

    Each

    lbl As

    Label In

    table1.Header
  62. Dim

    cell As

    WritableCell
  63. cell.InitializeText(col, 0, lbl.Text)
  64. cell.SetCellFormat(cellFormat)
  65. sheet1.AddCell(cell)
  66. sheet1.SetColumnWidth(col, 15)
  67. col = col + 1
  68. Next
  69. sheet1.SetColumnWidth(1, 40)
  70. sheet1.SetRowHeight(0, 15)
  71. 'add the data
  72. Dim

    rowsFormat As

    WritableCellFormat
  73. rowsFormat.Initialize
  74. rowsFormat.HorizontalAlignment = rowsFormat.HALIGN_CENTRE
  75. For

    col = 0 To

    table1.NumberOfColumns - 1
  76. For

    row = 0 To

    table1.Size - 1
  77. Dim

    cell As

    WritableCell
  78. cell.InitializeText(col, row + 1, table1.GetValue(col, row))
  79. cell.SetCellFormat(rowsFormat)
  80. sheet1.AddCell(cell)
  81. Next
  82. Next
  83. 'Must call write AND close To save the data.
  84. newWorkbook.Write
  85. newWorkbook.Close
  86. End

    Sub
  87. Sub

    Table1_CellClick(Col As

    Int, Row As

    Int)
  88. table1.SetValue(Col, Row, "xxx"

    )
  89. End

    Sub
  90. Sub

    Activity_Resume

  91. End

    Sub

  92. Sub

    Activity_Pause (UserClosed As

    Boolean

    )

  93. End

    Sub

Tomorrow I will post the complete code for this Excel File Reader Application in Android considering on how to have a formatting in Excel like TableSettings, SetColumnsWidths, ScrollChanged, AddRow, AddColumns, RemoveRow, HideRows, and many more functions to create an Excel File Application.

Best regards,

Engr. Lyndon R. Bermoy
IT Instructor/System Developer/Mobile Developer
09126450702
[email protected]

Follow and add me in my Facebook Account: www.facebook.com/donzzsky

Visit my page on Facebook at: https://www.facebook.com/BermzISware


Download
You must upgrade your account or reply in the thread to view hidden text.
 

452,292

323,341

323,350

Top