B4J Question Exporting settings/map file to an Excel document

trueboss323

Active Member
Licensed User
Longtime User
This is from my B4A thread here . I am using KeyValueStore2 to save user settings into the default file. I want to export the map or settings file it creates and be able to open it and manually edit settings in Excel. Then be able to convert it back to the original file format. With B4J , i'd like to create a program where it does it automatically for me. Any help please?
 

trueboss323

Active Member
Licensed User
Longtime User
KVS.ListKeys returns all the keys in the database. You need to get the values using these keys and write them to an Excel workbook: jPOI - Supports Microsoft Excel xls and xlsx Workbooks

Here's what i got so far:

B4X:
Private Sub CreateWorkbook
    Dim wb As PoiWorkbook
    wb.InitializeNew(True) 'create new xlsx workbook
    Dim sheet1 As PoiSheet = wb.AddSheet("Sheet 1", 0)
    Dim row As PoiRow = sheet1.CreateRow(0)

    Dim titleRow As PoiRow = sheet1.CreateRow(0) 'first row
    titleRow.CreateCellString(0, "key")
    titleRow.CreateCellString(1, "value")
  
    For Each key In kvs.ListKeys
        Dim row As PoiRow = sheet1.CreateRow(1) 'second row
        row.CreateCellString(0,key)
        row.CreateCellString(1,kvs.Get(key))

    Next

    wb.Save(File.DirApp, "1.xlsx")
    wb.Close
End Sub

It only gives me the last key in the array for the second row , instead of listing all the keys.
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
With KVS2 it's a bit different than from SQL. I'm not familiar with what format it is in, and KVS doesn't give me much options.
B4X:
Private Sub BuildKVS (Xls As String, Output As String)
    kvs.Initialize(fc.InitialDirectory, "datastore2")
    Try
        Dim wb As PoiWorkbook
        wb.InitializeExisting(Xls, "", "")
        Dim sheet As PoiSheet = wb.GetSheet(0)
        Dim headerRow As PoiRow = sheet.GetRow(0)
        Dim keylist As List
      
  
        For Each cell As PoiCell In headerRow.Cells
            Log(keylist)
        Next
        For Each row As PoiRow In sheet.Rows
            If row.RowNumber = 0 Then Continue
            Dim key As String
            For Each cell As PoiCell In row.Cells
                If cell.ColumnIndex = 0 Then
                    key = cell.ValueString
                Else
                End If
            Next
        Next
    '    sql.TransactionSuccessful
        wb.Close
        Log("Database created successfully!")
    Catch

        Log(LastException)
      
        Log("Error: " & LastException)

    End Try
 
End Sub
I'm a bit lost here.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
"datastore2" is a SqLite Database....

Open if in the usual way, check the table and their layout...

See Class-Source of kvs2 for more details
B4X:
'creates the main table (if it does not exist)
Private Sub CreateTable
    sql1.ExecNonQuery("CREATE TABLE IF NOT EXISTS main(key TEXT PRIMARY KEY, value NONE)")
End Sub
 
Upvote 0

trueboss323

Active Member
Licensed User
Longtime User
I still can't seem to figure it out. It just gives me more errors and problems. I don't even know if I'm doing it correctly:
B4X:
Dim kvs As KeyValueStore
kvs.Initialize(fc.InitialDirectory, "datastore2")

    Private sql As SQL
    File.Delete(TextField3.Text, "")
    sql.InitializeSQLite(TextField3.Text, "converted", True)
    sql.BeginTransaction
    Try
        sql.ExecNonQuery("CREATE TABLE data (key TEXT, value TEXT, PRIMARY KEY (key, key))")
        Dim wb As PoiWorkbook
        wb.InitializeExisting(Xls, "", "")
        Dim sheet As PoiSheet = wb.GetSheet(0)
        Dim keyRow As PoiRow = sheet.GetRow(0)
        If keyRow.IsInitialized = False Then
            cutils.ShowNotification2("", "Key row not found!", cutils.ICON_ERROR, MainForm)
            Return
        End If
        Dim langs As List
    For Each cell As PoiCell In keyRow.Cells
        If cell.ValueString.ToLowerCase = "value" Then Continue
            langs.Add(cell.ValueString)
        Next
        For Each row As PoiRow In sheet.Rows
            If row.RowNumber = 0 Then Continue
            Dim key As String
            For Each key In kvs.ListKeys
                If cell.ColumnIndex = 0 Then
                    kvs.Put(key, kvs.Get)
                Else
                    Dim lang As String = langs.Get(cell.ColumnIndex - 1)
                    sql.ExecNonQuery2("INSERT INTO data VALUES (?, ?, ?)", Array (key.ToLowerCase, lang, cell.ValueString))
                End If
            Next
        Next
        sql.TransactionSuccessful
        wb.Close
        Log("Finished!")
 
Upvote 0
Top