当前位置: 首页 > 知识库问答 >
问题:

Jasper报告与JRBeanGrou

胡志
2023-03-14

我已经使用JRResultSetDataSource使用了Jasper报告,但这是我第一次使用JRBeanGrou

我正在尝试使用以下代码运行报告测试:

客户.java

public class Cliente {
    private String nomeCliente;

    public String getNomeCliente() {
        return nomeCliente;
    }

    public void setNomeCliente(String nomeCliente) {
        this.nomeCliente = nomeCliente;
    }
}

Report.java

public class Report<T> {
    private Collection<T> dataList;
    private String jrxml;

    public Report() {
    }

    public Report(Collection<T> dataList, String jrxml) {
        super();
        this.dataList = dataList;
        this.jrxml = jrxml;
    }

    public Collection<T> getDataList() {
        return dataList;
    }

    public void setDataList(Collection<T> dataList) {
        this.dataList = dataList;
    }

    public String getJrxml() {
        return jrxml;
    }

    public void setJrxml(String jrxml) {
        this.jrxml = jrxml;
    }

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public void getReport(Collection<T> dataList, String jrxml) throws Exception {
        JasperReport report = null;
        JasperDesign reportDesign;

        try {
            reportDesign = JRXmlLoader.load(jrxml);
            report = JasperCompileManager.compileReport(reportDesign);

        } catch (JRException e2) {
            e2.printStackTrace();
        }

        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

        Map params = new HashMap();
        params.put("dataSource", dataList);

        JasperPrint jsPrint = null;

        try {
            jsPrint = JasperFillManager.fillReport(report, params, beanColDataSource);
        } catch (JRException e1) {
            e1.printStackTrace();
        }

        JasperViewer viewer = new JasperViewer(jsPrint, false);
        viewer.setVisible(true);
    }
}

clientereport.jrxml

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.6.0.final using JasperReports Library version 6.6.0  -->
<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="estiloinstitutobeleza_report" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="05a86d77-8b2c-49cc-9bf6-27fbf7a545d5">
    <property name="ireport.zoom" value="1.0"/>
    <property name="ireport.x" value="0"/>
    <property name="ireport.y" value="0"/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="One Empty Record"/>
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TH" mode="Opaque" backcolor="#F0F8FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_CH" mode="Opaque" backcolor="#BFE1FF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <style name="Table 1_TD" mode="Opaque" backcolor="#FFFFFF">
        <box>
            <pen lineWidth="0.5" lineColor="#000000"/>
            <topPen lineWidth="0.5" lineColor="#000000"/>
            <leftPen lineWidth="0.5" lineColor="#000000"/>
            <bottomPen lineWidth="0.5" lineColor="#000000"/>
            <rightPen lineWidth="0.5" lineColor="#000000"/>
        </box>
    </style>
    <subDataset name="Dataset1" uuid="d50d967c-8a6d-4e1e-b44e-49261df940e9">
        <queryString>
            <![CDATA[]]>
        </queryString>
        <field name="nomeCliente" class="java.lang.String"/>
    </subDataset>
    <parameter name="dataSource" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
    <field name="id" class="java.lang.Integer"/>
    <field name="nome" class="java.lang.String"/>
    <field name="sexo" class="java.lang.Integer"/>
    <field name="datanascimento" class="java.lang.String"/>
    <field name="logradouro" class="java.lang.String"/>
    <field name="numero" class="java.lang.Integer"/>
    <field name="complemento" class="java.lang.String"/>
    <field name="bairro" class="java.lang.String"/>
    <field name="cidade" class="java.lang.String"/>
    <field name="uf" class="java.lang.Integer"/>
    <field name="cep" class="java.lang.String"/>
    <field name="telefone" class="java.lang.String"/>
    <field name="celular1" class="java.lang.String"/>
    <field name="celular2" class="java.lang.String"/>
    <field name="email1" class="java.lang.String"/>
    <field name="email2" class="java.lang.String"/>
    <group name="nome">
        <groupExpression><![CDATA[$F{nomeCliente}]]></groupExpression>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="23" splitType="Stretch">
            <staticText>
                <reportElement x="202" y="0" width="167" height="23" uuid="6ec26fa8-d021-455c-8bf5-b2716a0c7919"/>
                <textElement>
                    <font size="16" isBold="true"/>
                </textElement>
                <text><![CDATA[Relatório de Clientes]]></text>
            </staticText>
            <image>
                <reportElement x="1" y="-20" width="172" height="43" uuid="d382676c-4568-4b4f-b9a6-6e4741aab5ee"/>
                <imageExpression><![CDATA["images/EstiloInstitutoBelezaLogo.png"]]></imageExpression>
            </image>
        </band>
    </title>
    <pageHeader>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement x="435" y="0" width="80" height="20" uuid="06d08240-2804-45f3-8c68-199839537286"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Página "+$V{PAGE_NUMBER}+" de"]]></textFieldExpression>
            </textField>
            <textField evaluationTime="Report">
                <reportElement x="515" y="0" width="40" height="20" uuid="9db45469-0ec8-4a69-8e02-95ecd71639ce"/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
        </band>
    </pageHeader>
    <columnHeader>
        <band height="14" splitType="Stretch">
            <line>
                <reportElement x="0" y="13" width="555" height="1" uuid="3d9ff8e2-9dfa-4e1a-bf04-4c08222532aa"/>
            </line>
            <line>
                <reportElement x="198" y="0" width="1" height="13" uuid="3ccdd4cc-2dcc-4547-981a-7dd0c0795ef3"/>
            </line>
            <line>
                <reportElement x="300" y="0" width="1" height="13" uuid="2c50fa85-487d-4595-9144-f78adb6d8f32"/>
            </line>
            <line>
                <reportElement x="398" y="0" width="1" height="13" uuid="856b71be-60fd-450f-933e-b092ef412abb"/>
            </line>
            <staticText>
                <reportElement x="0" y="0" width="202" height="14" uuid="36ac836a-63d7-47b5-82b1-c2fb15034dc6"/>
                <text><![CDATA[Nome]]></text>
            </staticText>
            <staticText>
                <reportElement x="202" y="0" width="100" height="14" uuid="8ff1d64c-ae30-4c97-a544-c6eb1989ce05"/>
                <text><![CDATA[Data de Nascimento]]></text>
            </staticText>
            <staticText>
                <reportElement x="402" y="0" width="100" height="14" uuid="01153fec-09ae-4945-b3cb-04c254fb0e65"/>
                <text><![CDATA[Celular]]></text>
            </staticText>
            <staticText>
                <reportElement x="302" y="0" width="100" height="14" uuid="5ab92c2e-f72c-442a-b321-d84841953aa3"/>
                <text><![CDATA[Telefone]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="205" splitType="Stretch">
            <line>
                <reportElement x="0" y="13" width="555" height="1" uuid="ab35d598-27d4-4410-a86a-0876cf839aef"/>
            </line>
            <line>
                <reportElement x="198" y="0" width="1" height="13" uuid="1dd11b36-1226-4917-a2b5-ae60c5c3ef8b"/>
            </line>
            <line>
                <reportElement x="300" y="0" width="1" height="13" uuid="abef3282-f707-4dd8-bd23-a0a07ef60fb6"/>
            </line>
            <line>
                <reportElement x="398" y="0" width="1" height="13" uuid="4b153f11-2e81-4f19-97d3-64f40cda5ffb"/>
            </line>
            <componentElement>
                <reportElement x="1" y="0" width="554" height="13" uuid="21f104d0-3d32-4bf7-a5bf-a062dcb67f89">
                    <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/>
                    <property name="com.jaspersoft.studio.table.style.table_header" value="Table 1_TH"/>
                    <property name="com.jaspersoft.studio.table.style.column_header" value="Table 1_CH"/>
                    <property name="com.jaspersoft.studio.table.style.detail" value="Table 1_TD"/>
                </reportElement>
                <jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
                    <datasetRun subDataset="Dataset1" uuid="ec5065e1-b31a-4ae5-8ad9-574969eadcfb">
                        <datasetParameter name="REPORT_DATA_SOURCE">
                            <datasetParameterExpression><![CDATA[$P{dataSource}]]></datasetParameterExpression>
                        </datasetParameter>
                    </datasetRun>
                    <jr:column width="554" uuid="c5e7b9ad-85cf-40fe-aa2d-8f8af92bab6f">
                        <jr:detailCell style="Table 1_TD" height="30">
                            <textField>
                                <reportElement x="0" y="0" width="554" height="30" uuid="ed6e2650-5949-404b-a542-05e79b134580"/>
                                <textFieldExpression><![CDATA[$F{nomeCliente}]]></textFieldExpression>
                            </textField>
                        </jr:detailCell>
                    </jr:column>
                </jr:table>
            </componentElement>
        </band>
    </detail>
    <columnFooter>
        <band splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="20" splitType="Stretch">
            <textField evaluationTime="Report">
                <reportElement x="515" y="0" width="40" height="20" uuid="e564df6b-0f6f-4ea1-8a9d-aac36c0a1914"/>
                <textFieldExpression><![CDATA[" " + $V{PAGE_NUMBER}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement x="435" y="0" width="80" height="20" uuid="9cb91f0b-9fb2-475e-a59b-706eb9beccef"/>
                <textElement textAlignment="Right"/>
                <textFieldExpression><![CDATA["Página "+$V{PAGE_NUMBER}+" de"]]></textFieldExpression>
            </textField>
        </band>
    </pageFooter>
    <summary>
        <band splitType="Stretch"/>
    </summary>
</jasperReport>

Main.java

import java.util.ArrayList;
import java.util.List;

import br.com.cts.model.Cliente;
import br.com.cts.util.Report;

public class Main {

    public static void main(String[] args) throws Exception {
        Cliente cliente1 = new Cliente();
        cliente1.setNomeCliente("John");

        Cliente cliente2 = new Cliente();
        cliente2.setNomeCliente("Mike");

        List<Cliente> clientes = new ArrayList<>();
        clientes.add(cliente1);
        clientes.add(cliente2);

        Report<Cliente> report = new Report<Cliente>();
        report.getReport(clientes, "reports/clientereport.jrxml");
    }
}

我得到以下错误:

net.sf.jasperreports.engine.design.JRValidationException: Report design not valid : 
     1. Field not found : nomeCliente
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.verifyDesign(JRAbstractCompiler.java:280)
    at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:152)
    at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:357)
    at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:617)
    at br.com.cts.util.Report.getReport(Report.java:53)
    at br.com.cts.main.Main.main(Main.java:23)
Exception in thread "main" java.lang.NullPointerException
    at net.sf.jasperreports.engine.fill.JRFiller.createReportFiller(JRFiller.java:267)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:156)
    at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:145)
    at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:689)
    at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:1005)
    at br.com.cts.util.Report.getReport(Report.java:68)
    at br.com.cts.main.Main.main(Main.java:23)

我不知道我的代码有什么问题。

共有1个答案

穆正祥
2023-03-14

看起来您正在尝试添加不属于的额外图层。您正在向报表传递一个数据源,即 List

你的主要报告应该有

<field name="nomeCliente" class="java.lang.String" />

它不应该具有您拥有的所有其他字段名,除非它们也是Cliente的一部分。

因此,您会收到错误,因为字段名称不在字段列表中,就像错误所暗示的那样。拥有子数据集的一个原因是,如果主数据源中有另一个列表(客户端中的另一个属性是列表

 类似资料:
  • 我正在从. jasper文件加载jasper报告文件以提高性能,如下所示。 但是我现在有一个要求,动态形成的查询必须设置为. jasper(编译)文件。有没有办法做同样的事情?

  • 我试图做一个Jasper报表簿,稍后填充它的内容,并保持封面不变,但是当我使用与报表簿对应的. jasper时,我得到了一个空白页。我一个接一个地传递这本书的页面,它起作用了,但是当我传递整本书时,我什么也没得到。 有人知道如何在java中使用整个Jasper报告簿来生成PDF吗? 谢谢。

  • 我试图在jrxml中创建表格。表格中的列数可能会很大,当我试图以PDF格式导出报告时,由于列数很大,数据变得不可读。有什么方法可以让我们以更大的页面尺寸(如A3)导出报告,或者在A4尺寸上增加滚动功能。注意:报告只需要导出为pdf格式,而不是CSV或任何其他格式。 谢谢,任何帮助感谢。

  • 当简单报表不适用于子报表时,此操作有效...

  • 我有一个使用JSF和PrimeFaces开发的项目。我需要用Jasper在PDF上显示条形码。我怎样才能做到这一点?

  • 问题内容: 我有一个带有下拉菜单的Web应用程序,用户可以从中选择报表的类型。report1,report2,report3等。 基于所选的报告,Jasper报告将在服务器上编译并以PDF格式作为弹出窗口打开。 在服务器端,我使用下面的代码(例如对于report1)使用一种单独的方法来实现每个报告: 同样,report2在具有以下代码的单独方法中: 现在,我有一个要求,如果从下拉列表中选择了rep