vigamage vigamage - 1 month ago 16
Java Question

set values from outside the report to a jasper report

I am going to generate reports using jasper reports. For that, my plan is,


  • create the
    jrxml
    file as needed using
    iReport
    tool.

  • set the values required from out side the report. (I mean if I have a variable called
    name="james"
    in my
    java
    program set that variable to a text field in the report)

  • then create the report and generate the pdf.



What I need to know is the possibility of this. Can I set my variables into the fields in my
jrxml
file? If it is possible, How can I do it?

Answer

What I need to know is the possibility of this. Can I set my variables into the fields in my jrxml file?

Basically, yes.

You need to define the parameter as a "Parameter" type in the report, then supply the parameter as the "Text Field Expression" on the form in the format $P{...}

Prameters

Then, when you want to fill the report, you need to do is create a Map of some kind, each key in the map should be the name of parameter your defined in the report, for example...

Map<String, Object> mapParameters = new HashMap<String, Object>(5);
mapParameters.put("USER_NAME", name);

JasperPrint print = JasperFillManager.fillReport(report, mapParameters);