如何通过jrxml创建动态报表? [英] How to create a dynamic report thorough jrxml?

查看:103
本文介绍了如何通过jrxml创建动态报表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 jrxml 创建动态报告。我已对列进行参数化,即该报告的 jrxml 也可用于生成其他报告。

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.

但是,我还没有设法让字段变得灵活。也就是说,如果用户选择4列它将起作用,但如果选择1或2或3列,则由于字段名称未被识别而产生错误。

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.

如果可以创建类似fieldname的默认表达式或者可以使用for循环/ java脚本,请紧急发布解决方案。

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?

jrxml如下:

<?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>


推荐答案

如果您希望它是动态的,就像隐藏一样/显示新字段,我认为唯一的方法是在运行时修改你的jrxml并编译它。

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.

更新:

要使JasperReport在运行时动态,您有三种方法:

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

1。在iReport中创建JRXML

如果你的报告很复杂并且有很多子报告和子数据集,那么最好采用这种方法,因为iReport会让你更快更容易地设计和维护。

当遵循这种方法时,您将创建jrxml文件并存储在类路径中,在运行时,您将加载此文件,打开它,并修改所需的XML标记。我建议从包含最大列数的jrxml开始,然后如果用户选择较少的列,则在jrxml中找到那些额外的列并删除它们。

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。用Java创建整个报告

如果您的报告非常简单,如快速表格报告,这种方法更好,在这种情况下您不需要jrxml文件,您可以创建整个报告使用JasperReport库API从头开始运行。这种方法会使编写报告更加困难,因为在编译和运行之前,您将看不到任何内容。更糟糕的是它的维护噩梦。

您的最终Java源代码如下所示:固定柱宽测试 1

3。使用DynamicJasper

最后,如果您对新工具持开放态度,则始终 DynamicJasper ,您可以在线查看演示,它非常酷。

我最后让DJ给你看所有的方法,DJ实际上使用了第二个。即时创建报告的Java源代码。

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:固定列宽测试报告版权归DynamicJasper在这里仅作为样本使用。

这篇关于如何通过jrxml创建动态报表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆