JasperReport组件

时间:2011-11-08 16:26:26

标签: java reporting jasper-reports

JasperReports的新手,并试图:(a) manually write a JRXML(b) manually program a JasperDesign myself (not using a template)。我的理解是XML和Java对象都代表了相同的概念:一个空的" shell"没有任何数据的报告。

我试图弄清楚哪些JR组件符合我的需求,referencesamples以及许多在线搜索都没有回头很明确。大多数JR文档似乎,至少对新手来说,似乎是从一个假设的知识和#34;观点出发

以下"组件" 之间有什么区别和适当的用法(不确定还有什么可以称之为)

  • 文本
  • 的TextField
  • 的TextElement
  • 静态文本

这个images example here显示了大部分这些元素被使用,看起来似乎是一种不那么明显的格式。

我的问题的根源是我希望显示两种类型的基于文本的信息;一个我指的是"字段" (不要与JR领域混淆)和另一个我打电话"文本块"由头部和身体组成。我希望这些字段/文本块呈现如下:

"现场":

Name:          John Smith
Age:           42
Summary:       This is an example of a field

"文本块":

Name:
John Smith

Age:
42

Summary:
This is an example of a text block. "Summary:" is the head, and this is the body.

相同的信息,只是以不同的方式呈现。我相信上面列出的一个/几个JR组件是我需要使用的组件,但我似乎无法找到任何确认/拒绝该组件的文档。

有人对此有任何想法吗?提前谢谢!

2 个答案:

答案 0 :(得分:1)

你是对的,.jrxmlJasperDesign对象是类似的,代表一个空的报表设计。然后可以将此设计编译为.jasper文件或JasperReport对象,然后填充数据以生成实际报告。

在您列出的四个“组件”中,只有TextFieldStaticText才是报告组件。其他人只是拥有父元素的属性。

  • StaticText包含永不改变的文字。它设置在设计中就是这样。
  • TextField有一个表达式,在填写报表时执行。这些是您将数据放入报表的元素。
  • Text是一个标记,用于保存StaticText元素的实际文本内容。这是它出现的唯一地方。
  • TextElement声明特定于报表应如何呈现文本的属性。每个TextFieldStaticText都可以包含此标记。属性包括文本对齐,旋转,字体,字体大小等。

要解决您的问题,您需要使用StaticText组件作为名称,年龄和摘要标签,因为它们将始终相同;和实际报告数据的TextField组件。

正如其他人所说,我会推荐iReport。我会使用它进行大部分设计,然后手工制作和更改JRXML(它产生的xml有点膨胀)。设计人员将隐藏TextTextElement,看起来好像是在文本组件本身上设置了属性。这不是问题,但是如果您要手动更改JRXML,您应该注意它,这样您就不会将属性添加到错误的标签中。查看iReport的输出也是学习有效JRXML的好方法,因为您获取的错误.jrxml文件的错误消息并不总是有用。

希望有所帮助!

答案 1 :(得分:0)

如果您不想使用iReport,可以尝试使用DynamicJasper Java API或JasperReports Java API动态构建报告。

我给你的建议:不要试图重新发明轮子。尝试使用iReport设计器,它是构建简单和复杂报表的非常有用的工具。
使用iReport只需一个交叉:JasperServer应用程序(可供使用的Web应用程序部署,构建报告并将数据导出为不同格式)可与jrxml模板配合使用。

iReport Ultimate GuideJasperReports Ultimate Guide是非常有价值的书籍。您可以找到一些手册here

这是与您的问题相关的jrxml模板片段(我希望)。它是用iReport编写的。

<detail>
    <band height="60" splitType="Stretch">
        <staticText>
            <reportElement x="0" y="0" width="63" height="20"/>
            <box leftPadding="10">
                <topPen lineWidth="1.0"/>
                <leftPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[Name:]]></text>
        </staticText>
        <staticText>
            <reportElement x="0" y="20" width="63" height="20"/>
            <box leftPadding="10">
                <leftPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[Age:]]></text>
        </staticText>
        <staticText>
            <reportElement x="0" y="40" width="63" height="20"/>
            <box leftPadding="10">
                <leftPen lineWidth="1.0"/>
                <bottomPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <text><![CDATA[Summary:]]></text>
        </staticText>
        <textField>
            <reportElement x="63" y="0" width="290" height="20"/>
            <box leftPadding="10">
                <topPen lineWidth="1.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="63" y="20" width="290" height="20"/>
            <box leftPadding="10">
                <pen lineWidth="1.0"/>
                <topPen lineWidth="0.0"/>
                <leftPen lineWidth="0.0"/>
                <bottomPen lineWidth="0.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Age}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement x="63" y="40" width="290" height="20"/>
            <box leftPadding="10">
                <bottomPen lineWidth="1.0"/>
                <rightPen lineWidth="1.0"/>
            </box>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Summary}]]></textFieldExpression>
        </textField>
    </band>
</detail>

结果将是: enter image description here

我只花了5分钟。

相关问题