By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,952 Members | 1,078 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,952 IT Pros & Developers. It's quick & easy.

How to create dynamic report through JRXML???

P: 13
Hey! I am working on jrxml to create dynamic re0ports. I have parameterized the columns i.e. the jrxml for that report can be used to generate other reports as well.

However, i have not managed to make the fields flexible. That is, if the user selects 4 columns it would work but if 1 or 2 or 3 columns are selected, it gives an error since the field names are unidentified.

Please post a solution urgently if something like a default expression for fieldname can be created or a for loop/java script can be used.

Moreover, how can jasper designer be exactly used to achieve this?

The jrxml is as follows:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <!DOCTYPE jasperReport
  3.   PUBLIC "-//JasperReports//DTD Report Design//EN"
  4.   "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
  5.  
  6. <jasperReport name="report1">
  7.  
  8.     <parameter name="reportTitle" class="java.lang.String"/>
  9.     <parameter name="author" class="java.lang.String"/>
  10.     <parameter name="startDate" class="java.lang.String"/>
  11.  
  12.  <parameter name="C1" class="java.lang.String">
  13.         <defaultValueExpression>
  14.             new java.lang.String("")
  15.         </defaultValueExpression>
  16.     </parameter>
  17.  
  18.     <parameter name="C2" class="java.lang.String">
  19.         <defaultValueExpression>
  20.             new java.lang.String("")
  21.         </defaultValueExpression>
  22.     </parameter>
  23.  
  24.     <parameter name="C3" class="java.lang.String">
  25.         <defaultValueExpression>
  26.             new java.lang.String("")
  27.         </defaultValueExpression>
  28.     </parameter>
  29.  
  30.     <parameter name="C4" class="java.lang.String">
  31.         <defaultValueExpression>
  32.             new java.lang.String("default parameter value")
  33.         </defaultValueExpression>
  34.     </parameter>
  35.  
  36.     <field name="COLUMN_1" class="java.lang.Integer"/>
  37.     <field name="COLUMN_2" class="java.lang.Integer"/>
  38.     <field name="COLUMN_3" class="java.lang.Integer"/>
  39.     <field name="COLUMN_4" class="java.lang.Integer"/>
  40.  
  41.     <title>
  42.         <band height="60">
  43.             <textField>
  44.                 <reportElement x="0" y="10" width="500" height="40"/>
  45.                 <textElement textAlignment="Center">
  46.                     <font size="24"/>
  47.                 </textElement>
  48.                 <textFieldExpression class="java.lang.String">
  49.                     <![CDATA[$P{reportTitle}]]>
  50.                 </textFieldExpression>
  51.             </textField>
  52.             <textField>
  53.                 <reportElement x="0" y="40" width="500" height="20"/>
  54.                 <textElement textAlignment="Center"/>
  55.                 <textFieldExpression class="java.lang.String">
  56.                     <![CDATA["Run by: " + $P{author}
  57.                         + " on " + $P{startDate}]]>
  58.                 </textFieldExpression>
  59.             </textField>
  60.         </band>
  61.     </title>
  62.  
  63.  
  64. <columnHeader>
  65.         <band height="30">
  66.             <rectangle>
  67.                 <reportElement x="0" y="0" width="500" height="25"/>
  68.                 <graphicElement/>
  69.             </rectangle>
  70.  
  71.             <textField>
  72.                 <reportElement x="0" y="5" width="170" height="15"/>
  73.                 <textFieldExpression class="java.lang.String">
  74.                     <![CDATA[$P{C1}]]>
  75.                 </textFieldExpression>
  76.             </textField>
  77.  
  78.             <textField>
  79.                 <reportElement x="70" y="5" width="170" height="15"/>
  80.               <textFieldExpression class="java.lang.String">
  81.                     <![CDATA[$P{C2}]]>
  82.                 </textFieldExpression>
  83.             </textField>
  84.  
  85.             <textField>
  86.                 <reportElement x="150" y="5" width="150" height="15"/>
  87.               <textFieldExpression class="java.lang.String">
  88.                     <![CDATA[$P{C3}]]>
  89.                 </textFieldExpression>
  90.             </textField>
  91.  
  92.             <textField>
  93.                 <reportElement x="300" y="5" width="150" height="15"/>
  94.               <textFieldExpression class="java.lang.String">
  95.                     <![CDATA[$P{C4}]]>
  96.                 </textFieldExpression>
  97.  
  98.             </textField>
  99.  
  100.  
  101.             </band>
  102.     </columnHeader>
  103.  
  104.  
  105. <detail>
  106.         <band height="20">
  107.  
  108.             <textField>
  109.                 <reportElement x="5" y="0" width="50" height="15"/>
  110.                 <textElement/>
  111.                 <textFieldExpression class="java.lang.Integer">
  112.                     <![CDATA[$F{COLUMN_1}]]>
  113.                 </textFieldExpression>
  114.             </textField>
  115.  
  116.             <textField>
  117.                 <reportElement x="90" y="0" width="150" height="15"/>
  118.                 <textElement/>
  119.                 <textFieldExpression class="java.lang.Integer">
  120.                     <![CDATA[$F{COLUMN_2}]]>
  121.                 </textFieldExpression>
  122.             </textField>
  123.  
  124.             <textField>
  125.                 <reportElement x="170" y="0" width="50" height="15"/>
  126.                 <textElement/>
  127.                 <textFieldExpression class="java.lang.Integer">
  128.                     <![CDATA[$F{COLUMN_3}]]>
  129.                 </textFieldExpression>
  130.             </textField>
  131.  
  132.             <textField>
  133.                 <reportElement x="320" y="0" width="150" height="15"/>
  134.                 <textElement/>
  135.                 <textFieldExpression class="java.lang.Integer">
  136.                     <![CDATA[$F{COLUMN_4}]]>
  137.                 </textFieldExpression>
  138.             </textField>
  139.  
  140.         </band>
  141.     </detail>
  142.  
  143. </jasperReport> 
Dec 20 '09 #1
Share this Question
Share on Google+
1 Reply


P: 1
I recommend to use DynamicReports, is open source and you can create dynamic reports very easy
Apr 14 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.