How to pass the SHEET_NAMES dynamically for XLS in jasper reports

srikotesh
srikotesh used Ask the Experts™
on
Hi Experts,

there are two approached to pass sheet names vaue
1st approach:

JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter( JRXlsAbstractExporterParameter.SHEET_NAMES, new String[] {"first","second","third"} ); 

Open in new window


cant we pass this value as parameter in java?

this way(1st approach) i am unable to follow because i am sheduling report for a specificdate. and passing few parameter inputs as well for the same report.

sample:
Util.getParamString(stringBuilder, "GENERATEDON", Date);//similarly few more params i am passing

Open in new window


2nd approach:

iam setting in the report properties like below(2nd approach): Property Name = "net.sf.jasperreports.export.xls.sheet.names.all"

 Property Value = "first/second/third"

is there any chance to pass this property value as dynamic.?

I have tried like below as not works for me

<line> 
<reportElement x="0" y="-1" width="1" height="1"> 
<propertyExpression name="net.sf.jasperreports.export.xls.sheet.name"><![CDATA[$F{course_name}]]></propertyExpression> 
</reportElement> 
</line>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Software Engineer
Commented:
Your second approach can work, but there are a couple of things to think through while getting it to work.

One thing to think is the "rendering time" of where you place that expression. The sheet name applies to the entire page, so you probably want to place it somewhere that gets rendered on a per page basis. (So placing it with the line of a single row of data may not work, but putting it in a page header or footer may.)

Another thing to think about it is when the variables you're using within the sheet name expression get evaluated. In your example above, you are using a field ($F{course_name}). I don't think that would work, because the field is being evaluated once for each row of data. This is unlikely to match up with the "rendering time" of the element in which you place the expression. Once you get that combination correct, your sheet names should start working.

To sum these up, make sure you have a variable ($V type) that gets evaluated once per page. Then use that variable in the sheet name expression within an element that is rendered once per page.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial