B4J Code Snippet jPOI clear a work sheet

I needed a way to re-use an Excel worksheet. The existing B4J library has no methods for this, and I couldn't find anything related on this forum. So I used the Java object to implement what I needed. The solution is based on StackOverflow suggestions.

B4X:
Sub clearSheet(wb As PoiWorkbook, sheetNum As Int)
    Dim theSheet As PoiSheet = wb.GetSheet(sheetNum)
    Dim jo As JavaObject = theSheet
    For j = theSheet.LastRowNumber To 0 Step -1
        Dim aRow As PoiRow = theSheet.GetRow(j)
        If aRow.isinitialized Then jo.RunMethod("removeRow", Array(aRow))
    Next
    Dim nregions As Int = jo.RunMethod("getNumMergedRegions", Array())
    For j = nregions-1 To 0 Step -1
        jo.RunMethod("removeMergedRegion", Array(j))
    Next
    Log("Number of rows = " & theSheet.LastRowNumber)
    Log("Number of merged regions = " & jo.RunMethod("getNumMergedRegions", Array()))
End Sub
 
Top