B4J Tutorial Excel Charts and Templates with jPOI library


Staff member
Licensed User
The jPOI library allows you to read and write Excel workbooks: http://www.b4x.com/android/forum/th...t-excel-xls-and-xlsx-workbooks.57392/#content

The library doesn't support creating new charts (or directly modifying existing charts).

You can read an Excel file with a chart, modify the data and write it. This is a simple and good solution if the data range is always the same.
However if the data range can change then you need to use dynamic named ranges.
This requires a bit more work but it is not too complicated.

First we start with the template file:

There are two defined names. Open Template.xlsx from the attached project to see them.
Press Ctrl + F3 to open the names editor:

(Step by step tutorial about dynamic named ranges: https://support.microsoft.com/en-us/kb/183446)

The actual ranges are not really important as they will be later overwritten. They must refer to a function (such as OFFSET) to be dynamic.

The program will:
  • Read the template file.
  • Fill the data.
  • Update the two ranges based on the number of items.
  • Save the workbook as a new file.
Updating the ranges is done with this code:
Private Sub SetNamedRange(wb As PoiWorkbook, SheetName As String, Name As String, Cells As String)
   Dim jo As JavaObject = wb
   Dim NameJO As JavaObject = jo.RunMethod("getName", Array(Name))
   NameJO.RunMethod("setRefersToFormula", Array($"${SheetName}!${Cells}"$))
End Sub
SetNamedRange(wb, "Sheet1", "Range_Name", "$C$5:$C$" & (numberOfItems + 4))
The result:




Well-Known Member
Licensed User

Does jPOI could use in Linux/Debian system ?


Active Member
Licensed User
Thank you for your help,but in this case i lose all setting for this cell llike border and other previous setting. There is no other method to simply replace the cell value?


New Member
Licensed User
Erel, This is great.
Am intersted in converting XL Chart to Android App...Have B4A Licence...Please suggest modification from B4J to B4A to implement this ...
Alternatively, can I invoke B4J in B4A environment ???
Thanks in advance.


Staff member
Licensed User
Alternatively, can I invoke B4J in B4A environment ???

This is quite a heavy library. In the past it wasn't possible to use it with B4A due to the dex size limits. You can try it now and set #MultiDex to True. Not the jPOI doesn't provide any user interface. For further discussion please start a new thread in B4A questions forum.