Overview

This section contains several examples demonstrating how to export collections with jXLS. In all samples we assume there are two Java beans of type Department and Employee

Simple collection export

This sample demonstrates how to export a collection of employees. Source code is as following

			Collection staff = new HashSet();
		        staff.add(new Employee("Derek", 35, 3000, 0.30));
		        staff.add(new Employee("Elsa", 28, 1500, 0.15));
		        staff.add(new Employee("Oleg", 32, 2300, 0.25));
		        staff.add(new Employee("Neil", 34, 2500, 0.00));
		        staff.add(new Employee("Maria", 34, 1700, 0.15));
		        staff.add(new Employee("John", 35, 2800, 0.20));
		        Map beans = new HashMap();
		        beans.put("employee", staff);
		        XLSTransformer transformer = new XLSTransformer();
		        transformer.transformXLS(templateFileName, beans, destFileName);
            

Template file screenshot

Screenshot of Employees XLS template file

You can also download Excel version employees.xls

Output file looks like

Screenshot of Employees XLS output

Excel file employees_output.xls

Complex bean export

This sample demonstrates how to export Department bean. Source code snippet

				Department department = new Department("IT");
				//...  initialization is skipped here
			        Map beans = new HashMap();
			        beans.put("department", department);
			        XLSTransformer transformer = new XLSTransformer();
			        // set grouping of rows for "department.staff" collection 
			        transformer.groupCollection("department.staff");
			        transformer.transformXLS(templateFileName, beans, destFileName);
            

Here is a template file screenshot

Screenshot of Department XLS template file

Excel version is here department.xls

Using this template we'll get an output like

Screenshot of Department XLS output

Excel is here department_output.xls

Multiple excel rows for every collection item

This sample demonstrates how to export collection with several rows per every collection item. Source code snippet

				Department department = new Department("IT");
				//...  initialization is skipped here
			        Map beans = new HashMap();
			        beans.put("department", department);
			        XLSTransformer transformer = new XLSTransformer();
			        transformer.transformXLS(templateFileName, beans, destFileName);
            

Here is a template file screenshot

Screenshot of multiplelistrows XLS template file

See excel file for details multiplelistrows.xls

Using this template we'll get an output like

Screenshot of multiplelistrows XLS output

Excel is here multiplelistrows_output.xls

Nested collections

This sample demonstrates how to export collections nested in other collections. We export collection of departments and every department contain staff collection. Source code snippet

				// create a list of Departments
			        List departments = new ArrayList();
				//... initialization
			        Map beans = new HashMap();
			        beans.put("departments", departments);
			        XLSTransformer transformer = new XLSTransformer();
			        transformer.transformXLS(templateFileName, beans, destFileName);
            

Template is

Screenshot of grouping XLS template file

See also xls file grouping.xls

Output is

Screenshot of grouping XLS output

Excel is here grouping_output.xls