Object Collection Sample

Introduction

This sample shows how to output a collection of Java objects into Excel with Jxls.

We will use a list of the following Employee objects to demonstrate how to output an object collection to Excel with Jxls.

public class Employee {
    private String name;
    private int age;
    private Double payment;
    private Double bonus;
    private Date birthDate;
    private Employee superior;

    // getters/setters
    ...
}

Report template

The report template for this example uses Comment markup to define the transformation areas. It looks like this

Object collection template

Java code

In this example we will use Jxls POI adapter to generate the report. The Java code is listed below along with comments explaining each line.

    List<Employee> employees = generateSampleEmployeeData();
    try(InputStream is = ObjectCollectionDemo.class.getResourceAsStream("object_collection_template.xls")) {
        try (OutputStream os = new FileOutputStream("target/object_collection_output.xls")) {
            Context context = new Context();
            context.putVar("employees", employees);
            JxlsHelper.getInstance().processTemplate(is, os, context);
        }
    }

Excel output

Final report for this example is shown on the following screenshot

Object collection output