Android Question export b4xtable to excel in b4a

mhk1368

Member
hi I found this example and want to use it in b4a.
but I could not find the libraries and similar code can any body help me?
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
thank you.
I check
This is code I have to write a Resultset to an Android .xls file. There is code in there that you will have to alter, but it will give a general idea how
to do this.

B4X:
Sub ResultSet2XL(oRS As ResultSet, tFF As tFolderAndFile, strSheetName As String, arrDataTypes() As String)
    
    Dim r As Int
    Dim c As Int
    Dim n As Int
    Dim iCols As Int = oRS.ColumnCount
    Dim oWWB As WritableWorkbook
    Dim oWS As WritableSheet
    Dim bCellFormatting As Boolean = IsArray(arrDataTypes)

    General.StartSW(3)
    
    oWWB.Initialize(tFF.strFolder, tFF.strFile)
    oWS = oWWB.AddSheet(strSheetName, 0)
    
    oRS.Position = 0
    For c = 0 To iCols - 1
        Dim oCell As WritableCell
        oCell.InitializeText(c, 0, oRS.GetColumnName(c))
        oWS.AddCell(oCell)
    Next
    
    oRS.Position = -1
    
    If bCellFormatting Then
        Do While oRS.NextRow
            r = r + 1
            For c = 0 To iCols - 1
                'Log("arrDataTypes(" & c & "): " & arrDataTypes(c))
                Dim oCell As WritableCell
                Select Case arrDataTypes(c)
                    Case "T"
                        oCell.InitializeText(c, r, oRS.GetString2(c))
                    Case "I", "XD"
                        oCell.InitializeNumber(c, r, oRS.GetInt2(c))
                    Case "R"
                        oCell.InitializeNumber(c, r, oRS.GetDouble2(c))
                    Case Else
                        oCell.InitializeText(c, r, oRS.GetString2(c))
                End Select
                oWS.AddCell(oCell)
            Next
            If r Mod 65530 = 0 Then 'in .xls sheet max rows = 65536
                n = n + 1
                r = 0
                Dim oWS As WritableSheet = oWWB.AddSheet(strSheetName & "_" & (n + 1), n)
                For c = 0 To iCols - 1
                    Dim oCell As WritableCell
                    oCell.InitializeText(c, r, oRS.GetColumnName(c))
                    oWS.AddCell(oCell)
                Next
            End If
        Loop
    Else
        Do While oRS.NextRow
            r = r + 1
            For c = 0 To iCols - 1
                Dim oCell As WritableCell
                oCell.InitializeText(c, r, oRS.GetString2(c))
                oWS.AddCell(oCell)
            Next
            If r Mod 65530 = 0 Then 'in .xls sheet max rows = 65536
                n = n + 1
                r = 0
                Dim oWS As WritableSheet = oWWB.AddSheet(strSheetName & "_" & (n + 1), n)
                For c = 0 To iCols - 1
                    Dim oCell As WritableCell
                    oCell.InitializeText(c, r, oRS.GetColumnName(c))
                    oWS.AddCell(oCell)
                Next
            End If
        Loop
    End If
    
    oWWB.Write
    oWWB.Close
    
    Dim rs As ResumableSub = Dialog.Show(Activity, Array As Object("OK"), _
                                         "SQL to .xls", "", _
                                        "SQL result written to:" & CRLF & CRLF & tFF.strFolder & tFF.strFile & _
                                        CRLF & CRLF & "Number of rows: " & oRS.RowCount & _
                                        CRLF & CRLF & "Sheet cell formatting: " & bCellFormatting & _
                                        CRLF & CRLF & "Time taken: " & General.FormatMilliSecs(General.StopSW(3)) , _
                                         -1, False, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Null, False, arrS, 1)
    Wait For (rs) Complete (strResult As String)
    
End Sub

RBS
 
Upvote 0

Brian Michael

Active Member
Licensed User
Hi, i also create a B4A example:

 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…