B4J Question Write to an existing xlsx worksheet

Tim Chapman

Active Member
Licensed User
Longtime User
I have searched the forum and can't find how to write to an existing .xlsx sheet within a workbook.
Does anyone know how to do this?

Thank you!
Tim.
 
Solution
Never mind. I figured it out. I hope this helps someone else.

Write to an existing spreadsheet:
Private Sub LogThreadToSpreadsheet
    Dim ThreadID1 As String = "Thread1"
    Dim CreatedAs As String = "2024Jan07"
    Dim Metadata As String = "whatever.you.want"
    Private XL As XLUtils
    XL.Initialize
    
    ' Define folder and filename
    Dim Folder As String = "D:\Downloads" ' Set your folder path here
    Dim FileName As String = "ThreadList.xlsx" ' Set your spreadsheet filename here

    'Create workbook and worksheet
    Dim workbook As XLWorkbookWriter = XL.CreateWriterFromTemplate(Folder, FileName)
    Dim sheet1 As XLSheetWriter = workbook.CreateSheetWriterByName("Threads")

    ' Find the next empty row
    Dim RowNum As Int
    RowNum =...

Tim Chapman

Active Member
Licensed User
Longtime User
Never mind. I figured it out. I hope this helps someone else.

Write to an existing spreadsheet:
Private Sub LogThreadToSpreadsheet
    Dim ThreadID1 As String = "Thread1"
    Dim CreatedAs As String = "2024Jan07"
    Dim Metadata As String = "whatever.you.want"
    Private XL As XLUtils
    XL.Initialize
    
    ' Define folder and filename
    Dim Folder As String = "D:\Downloads" ' Set your folder path here
    Dim FileName As String = "ThreadList.xlsx" ' Set your spreadsheet filename here

    'Create workbook and worksheet
    Dim workbook As XLWorkbookWriter = XL.CreateWriterFromTemplate(Folder, FileName)
    Dim sheet1 As XLSheetWriter = workbook.CreateSheetWriterByName("Threads")

    ' Find the next empty row
    Dim RowNum As Int
    RowNum = workbook.PoiWorkbook.GetSheetByName("Threads").LastRowNumber + 2
    Log("RowNum = " & RowNum)

    'Dim Row As PoiRow = workbook.PoiWorkbook.GetSheetByName("Threads").GetRow(RowNum)

    Log("Here")
    ' Write thread details into the new row

    sheet1.PutString(XL.AddressOne("A", RowNum), ThreadID1) ' Column A for ID
    Log("Here1")
    sheet1.PutString(XL.AddressOne("B", RowNum), CreatedAt) ' Column B for Created_At
    Log("Here2")
    sheet1.PutString(XL.AddressOne("C", RowNum), Metadata) ' Column C for Metadata
    Log("Here3")
    
    ' Save the workbook
    Dim f As String = workbook.SaveAs(Folder, FileName, True)
    Wait For (XL.OpenExcel(f)) Complete (Success As Boolean)
End Sub
 
Upvote 0
Solution
Top