2

I'm trying to use jXLS to export data from a list to an Excel sheet. I need to create an Excel template using jXLS and print out a list of data using that template. I have a Bean class called Department and I need to use a forEach statement to loop through the list and write data to the Excel sheet.

Can someone please tell me how and where I can write my Excel template? I know my code inside should look something like this -

            <jx:forEach items="${departments}" var="department">
                ${department.name} | ${department.chief}
            </jx:forEach>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Raaz
  • 125
  • 1
  • 5
  • 16
  • Have you checked the examples in "Reference" and "Samples" menu of [their own homepage](http://jxls.sourceforge.net/)? – BalusC Jun 05 '12 at 11:50
  • Yes, I have, and found that the template has to be written on the Excel sheet, and it would refer to the specific classes just as I had stated in my example above. However, I had difficulties with achieving repeating columns with this method, so had to abandon it! – Raaz Jun 18 '12 at 03:58

3 Answers3

1

You need to create an Excel Template file where in you define your basic structure which you need to repeat for number of objects in the collection.

The code

<jx:forEach items="${departments}" var="department">
                ${department.name} | ${department.chief}
            </jx:forEach>

will go in that template excel.

Then you need to use JXLS API in java code to generated the excel from this template.

Map contextBeans = new HashMap();
contextBeans.put("departments", departmentList);
xlsTransformer.transformXLS(xlsTemplateFileURL.getPath(), contextBeans, reportFileURL.getPath());

This code will create the excel file out of the template file populated with the collection loaded in contextBeans Map.

Sanjay Bharwani
  • 3,317
  • 34
  • 31
0

Syntactically jXLS is very similar to JSTL. in your case all you need is an Excel template that will have columns filled with jXLS notation like

   cola              col b
1  {department.name}  {department.chief}

and in Java you need a HashMap that will have an ArrayList of all of your department beans.

Brad Larson
  • 170,088
  • 45
  • 397
  • 571
sayannayas
  • 764
  • 9
  • 15
0

you can past them in your Excel template's sheet,any row is ok.Maybe you should look examples in http://jxls.sourceforge.net/ first.

Susie
  • 1