数据库表中的标题和详细信息

时间:2012-01-12 07:59:16

标签: java jasper-reports ireport

我需要一个编辑器来创建一些报告,所有报告都有标题(公司名称,地址,电话,图像),这些标题是从一个名为companies的表中读取的,并且有一个详细的,例如公司的所有客户。

我怎么能在iReports中这样做?

有没有办法在我的报告中链接两个表(公司和客户)?因为“报告查询”只允许我一个句子。

我使用Java Swing和MySql数据库。

感谢您的有用评论。

Report example

1 个答案:

答案 0 :(得分:1)

  

我需要一个编辑器来创建一些报告,所有报告都有标题(公司名称,地址,电话,图像),这些标题是从一个名为companies的表中读取的,并且有一个详细的,例如公司的所有客户。我怎么能在iReports中这样做?

您可以查看this samplesJasperReports distribution package包含许多其他示例(在文件夹%jasperreports%\ demo \ samples 中)。 iReport 还在文件夹%iReport%\ ireport \ samples 中有几个示例。

您的样本报告非常简单。

创建此类报告的步骤可以是这样的:

  1. 图像元素(带有公司徽标)和多个(或一个) staticText 元素(带有公司联系信息)添加到标题频段
  2. 使用字段( idClient name 地址)添加查询(用于从 MySQL 数据库获取数据)你的样本)
  3. 将带有列标题的 staticText 元素添加到列标题区域 - 它将是数据网格的列标题
  4. 将带有字段的 textField 元素添加到 Detail 频段 - 它将是网格中的数据
  5. 您应该阅读 JasperReports Ultimate Guide 。这是一个很棒的教程。

    示例 jrxml 文件(在 iReport 中设计):

    <?xml version="1.0" encoding="UTF-8"?>
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="sample_company" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
        <queryString>
            <![CDATA[SELECT idClient, name, address FROM customers_table]]>
        </queryString>
        <field name="idClient" class="java.lang.Integer"/>
        <field name="name" class="java.lang.String"/>
        <field name="address" class="java.lang.String"/>
        <title>
            <band height="108" splitType="Stretch">
                <staticText>
                    <reportElement x="171" y="53" width="183" height="55"/>
                    <textElement textAlignment="Center" verticalAlignment="Middle" markup="styled"/>
                    <text><![CDATA[<style isBold="true" forecolor="blue">ABC COMPANY</style>
    Main Avenue and 9th Street
    Tel: (593)  4 - 2066765
    e-mail: info@abc.com]]></text>
                </staticText>
                <image>
                    <reportElement x="245" y="3" width="41" height="50"/>
                    <imageExpression><![CDATA["abc_logo.png"]]></imageExpression>
                </image>
            </band>
        </title>
        <columnHeader>
            <band height="20">
                <staticText>
                    <reportElement x="11" y="0" width="160" height="20"/>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <text><![CDATA[Id Client]]></text>
                </staticText>
                <staticText>
                    <reportElement x="171" y="0" width="183" height="20"/>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <text><![CDATA[Name]]></text>
                </staticText>
                <staticText>
                    <reportElement x="354" y="0" width="172" height="20"/>
                    <textElement textAlignment="Center" verticalAlignment="Middle"/>
                    <text><![CDATA[Address]]></text>
                </staticText>
            </band>
        </columnHeader>
        <detail>
            <band height="20" splitType="Stretch">
                <textField>
                    <reportElement x="11" y="0" width="160" height="20"/>
                    <box leftPadding="10"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{idClient}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="354" y="0" width="172" height="20"/>
                    <box leftPadding="10"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
                </textField>
                <textField>
                    <reportElement x="171" y="0" width="183" height="20"/>
                    <box leftPadding="10"/>
                    <textElement/>
                    <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
    </jasperReport>
    

    结果将是(报告导出为 pdf 格式):

    The report in PDF format

    <强> 更新:

    要使用公司信息填充标题,您可以使用频段。您可以阅读有关群组here的信息。

    您的查询将是这样的:

    SELECT c.idClient, c.name, c.address, cmp.name AS companyName, cmp.contactInfo, cmp.id AS idCompany FROM customers c, companies cmp WHERE cmp.id=c.idCompany SORT BY cmp.id
    

    您还应该为来自 companies 表( companies.name {的数据添加报告的字段 {1}} companies.contactInfo ,例如):

    companies.id

    之后,您应该通过 <field name="companyName" class="java.lang.String"/> <field name="contactInfo" class="java.lang.String"/> <field name="idCompany" class="java.lang.String"/> idCompany 表中的唯一键)添加。 然后将带有字段( companies companyName )的 textField 元素添加到乐队。