How to export a ListObjectDataSource to excel with POI4XPAGES
This time we are going to use a List of Objects to export to Excel.
In this example we created a Java class 'Contact' which contains the attributes of the Contact like 'FirstName', 'LastName', 'City', etc with the related getter and setter methods for these fields.
We also created a method to get all Contacts as an ArrayList in a class defined in the WebContent\WEB-INF\faces-config.xml as a managed bean, so we can access the data within the export function.
After the configuration of the excel file is done, we can proceed to export our ListObjectDataSource which we are going to define in the ‘exportDefinition’
We can choose between data2rowexport and data2columnexport which represents the export direction.
· startRow: 0 based. Defines the row where the export should start Assign the dataSource:
· stepSize: 1 or higher. It’s possible to export a dataset to 1, 2 or more rows. (see columns for how to assign another row)
· buildValues: add a List of Objects. In this Case we call the method 'getAllContacts()' from our ContactBean which returns all Contacts as an ArrayList.Assign the columns:
Each column needs a columnDefinition.
· values - this parameter can be ignored as it is not in use anymore.
· columnNumber: the number of the column (0 based)
The export works with the same button we created for the Configuration export.
· columnTitle: the name of the field in the dataSource (it will be invoked with “get<columnTitle>” on the object in the list)
· rowShift: this defines how many rows down the value should be shifted (multiple row export per dataset)