How to create a dynamic report thorough jrxml?

2019-02-10 17:27发布

问题:

I am working on jrxml to create dynamic reports. 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:

<?xml version="1.0"?>
<!DOCTYPE jasperReport
  PUBLIC "-//JasperReports//DTD Report Design//EN"
  "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="report1">
    <parameter name="reportTitle" class="java.lang.String"/>
    <parameter name="author" class="java.lang.String"/>
    <parameter name="startDate" class="java.lang.String"/>

    <parameter name="C1" class="java.lang.String">
        <defaultValueExpression>
            new java.lang.String("")
        </defaultValueExpression>
    </parameter>
    <parameter name="C2" class="java.lang.String">
        <defaultValueExpression>
            new java.lang.String("")
        </defaultValueExpression>
    </parameter>
    <parameter name="C3" class="java.lang.String">
        <defaultValueExpression>
            new java.lang.String("")
        </defaultValueExpression>
    </parameter>
    <parameter name="C4" class="java.lang.String">
        <defaultValueExpression>
            new java.lang.String("default parameter value")
        </defaultValueExpression>
    </parameter>

    <field name="COLUMN_1" class="java.lang.Integer"/>
    <field name="COLUMN_2" class="java.lang.Integer"/>
    <field name="COLUMN_3" class="java.lang.Integer"/>
    <field name="COLUMN_4" class="java.lang.Integer"/>

    <title>
        <band height="60">
            <textField>
                <reportElement x="0" y="10" width="500" height="40"/>
                <textElement textAlignment="Center">
                    <font size="24"/>
                </textElement>
                <textFieldExpression class="java.lang.String">
                    <![CDATA[$P{reportTitle}]]>
                </textFieldExpression>
            </textField>
            <textField>
                <reportElement x="0" y="40" width="500" height="20"/>
                <textElement textAlignment="Center"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA["Run by: " + $P{author}
                        + " on " + $P{startDate}]]>
                </textFieldExpression>
            </textField>
        </band>
    </title>

    <columnHeader>
        <band height="30">
            <rectangle>
                <reportElement x="0" y="0" width="500" height="25"/>
                <graphicElement/>
            </rectangle>

            <textField>
                <reportElement x="0" y="5" width="170" height="15"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA[$P{C1}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="70" y="5" width="170" height="15"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA[$P{C2}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="150" y="5" width="150" height="15"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA[$P{C3}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="300" y="5" width="150" height="15"/>
                <textFieldExpression class="java.lang.String">
                    <![CDATA[$P{C4}]]>
                </textFieldExpression>
            </textField>
        </band>
    </columnHeader>

    <detail>
        <band height="20">
            <textField>
                <reportElement x="5" y="0" width="50" height="15"/>
                <textElement/>
                <textFieldExpression class="java.lang.Integer">
                    <![CDATA[$F{COLUMN_1}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="90" y="0" width="150" height="15"/>
                <textElement/>
                <textFieldExpression class="java.lang.Integer">
                    <![CDATA[$F{COLUMN_2}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="170" y="0" width="50" height="15"/>
                <textElement/>
                <textFieldExpression class="java.lang.Integer">
                    <![CDATA[$F{COLUMN_3}]]>
                </textFieldExpression>
            </textField>

            <textField>
                <reportElement x="320" y="0" width="150" height="15"/>
                <textElement/>
                <textFieldExpression class="java.lang.Integer">
                    <![CDATA[$F{COLUMN_4}]]>
                </textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

回答1:

In case you want it to be dynamic as in hiding/showing new fields, i think the only way would be to modify your jrxml at runtime and compile it.

UPDATE:

To make the JasperReport dynamic at runtime you have three approaches:

1. Create JRXML in iReport
If your report is complicated and have many subreports and subdatasets, its better to go this approach, since iReport will make it quicker and easier for you to design and maintain later.
When following this approach you will create the jrxml file and store in the classpath, at runtime you will load this file, open it, and modify the XML tags you want. I would recommend to start with a jrxml that includes the max number of columns, and then if user select less columns then find those additional columns in the jrxml and delete them.

2. Create whole report in Java
This approach is better if your report is really simple, like a quick tabular report, in this case you dont need a jrxml file, you can create the whole report at runtime from scratch using JasperReport library APIs. This approach will make designing the report harder as you wont see anything until you compile and run. Yet worse its a maintenance nightmare.
Your final Java source code will look something like this: "Fixed Column Width Test"1

3. Use DynamicJasper
Finally, if you are open to new tools, there is always DynamicJasper, you can check the online demo as well, its pretty cool.
I kept DJ for the end to show you all the approaches, DJ actually uses the second one. Creating Java source code of the report on the fly.


1: Fixed Column Width Test Report copyrights are for DynamicJasper and used here just as a sample.