繁体   English   中英

通过从JavaBean传递值来生成报告及其子报告

[英]Generating report and its subreport by passing value from JavaBean

我正在使用iReportJasperReports通过传递JavaBean类中的值来生成Report和Subreport

运行文件时出现以下错误

net.sf.jasperreports.engine.JRException: Could not load object from location : nullemailReport.jasper
at net.sf.jasperreports.engine.util.JRLoader.loadObjectFromLocation(JRLoader.java:267)
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluateSubreport(JRFillSubreport.java:310)
at net.sf.jasperreports.engine.fill.JRFillSubreport.evaluate(JRFillSubreport.java:259)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:275)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:426)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:1380)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:692)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportStart(JRVerticalFiller.java:255)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:113)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:879)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:801)
at net.sf.jasperreports.engine.fill.JRFiller.fillReport(JRFiller.java:89)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:601)
at com.f1soft.exportpdf.exportpdf.ExportUser.setAttachment(ExportUser.java:58)
at com.f1soft.exportpdf.exportpdf.ExportUser.main(ExportUser.java:119)

我的.jrxml报告代码是这样的

<?xml version="1.0" encoding="UTF-8"  ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
         name="userReport"
         columnCount="1"
         printOrder="Vertical"
         orientation="Portrait"
         pageWidth="595"
         pageHeight="842"
         columnWidth="535"
         columnSpacing="0"
         leftMargin="30"
         rightMargin="30"
         topMargin="20"
         bottomMargin="20"
         whenNoDataType="NoPages"
         isFloatColumnFooter="true"
         isTitleNewPage="true"
         isSummaryNewPage="true"
         isIgnorePagination="true">
    <property name="ireport.scriptlethandling" value="0" />
    <property name="ireport.encoding" value="UTF-8" />
    <import value="java.util.*" />
    <import value="net.sf.jasperreports.engine.*" />
    <import value="net.sf.jasperreports.engine.data.*" />

    <parameter name="imgLogoimgLogoimgLogo" isForPrompting="false" class="java.lang.String"/>
    <parameter name="SUBREPORT_DIR" isForPrompting="false" class="net.sf.jasperreports.engine.JasperReport"/>

    <field name="id" class="java.lang.Integer"/>
    <field name="name" class="java.lang.String"/>
    <field name="city" class="java.lang.String"/>

        <background>
            <band height="10"  isSplitAllowed="true" >
            </band>
        </background>
        <title>
            <band height="11"  isSplitAllowed="true" >
            </band>
        </title>
        <pageHeader>
            <band height="41"  isSplitAllowed="true" >
                <image  evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="4"
                        y="4"
                        width="114"
                        height="34"
                        key="image-1"/>
                    <box></box>
                    <graphicElement stretchType="NoStretch"/>
                    <imageExpression class="java.lang.String"><![CDATA[]]></imageExpression>
                </image>
            </band>
        </pageHeader>
        <columnHeader>
            <band height="10"  isSplitAllowed="true" >
            </band>
        </columnHeader>
        <detail>
            <band height="130"  isSplitAllowed="true" >
                <rectangle>
                    <reportElement
                        x="4"
                        y="4"
                        width="455"
                        height="24"
                        backcolor="#990000"
                        key="rectangle-1"/>
                    <graphicElement stretchType="NoStretch"/>
                </rectangle>
                <staticText>
                    <reportElement
                        x="9"
                        y="7"
                        width="41"
                        height="17"
                        forecolor="#FFFFFF"
                        key="staticText-1"/>
                    <box></box>
                    <textElement verticalAlignment="Middle">
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[ID]]></text>
                </staticText>
                <staticText>
                    <reportElement
                        x="57"
                        y="7"
                        width="114"
                        height="17"
                        forecolor="#FFFFFF"
                        key="staticText-2"/>
                    <box></box>
                    <textElement verticalAlignment="Middle">
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[Name]]></text>
                </staticText>
                <staticText>
                    <reportElement
                        x="179"
                        y="7"
                        width="114"
                        height="17"
                        forecolor="#FFFFFF"
                        key="staticText-3"/>
                    <box></box>
                    <textElement verticalAlignment="Middle">
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[City]]></text>
                </staticText>
                <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="9"
                        y="31"
                        width="41"
                        height="15"
                        key="textField-1"/>
                    <box></box>
                    <textElement>
                        <font/>
                    </textElement>
                <textFieldExpression   class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="57"
                        y="31"
                        width="114"
                        height="15"
                        key="textField-2"/>
                    <box></box>
                    <textElement>
                        <font/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$F{name}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="179"
                        y="31"
                        width="114"
                        height="15"
                        key="textField-3"/>
                    <box></box>
                    <textElement>
                        <font/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$F{city}]]></textFieldExpression>
                </textField>
                <line direction="TopDown">
                    <reportElement
                        x="4"
                        y="114"
                        width="516"
                        height="0"
                        backcolor="#0000CC"
                        key="line-1"/>
                    <graphicElement stretchType="NoStretch"/>
                </line>
                <subreport  isUsingCache="true">
                    <reportElement
                        x="4"
                        y="56"
                        width="381"
                        height="43"
                        key="subreport-1"/>
                    <subreportParameter  name="userid">
                        <subreportParameterExpression><![CDATA[$F{id}]]></subreportParameterExpression>
                    </subreportParameter>
                    <connectionExpression><![CDATA[$P{REPORT_CONNECTION}]]></connectionExpression>
                    <subreportExpression  class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "emailReport.jasper"]]></subreportExpression>
                </subreport>
            </band>
        </detail>
</jasperReport>

我的.jrxml SUB报告代码是这样的

<?xml version="1.0" encoding="UTF-8"  ?>
<!-- Created with iReport - A designer for JasperReports -->
<!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport
         name="emailReport"
         columnCount="1"
         printOrder="Vertical"
         orientation="Portrait"
         pageWidth="595"
         pageHeight="842"
         columnWidth="595"
         columnSpacing="0"
         leftMargin="0"
         rightMargin="0"
         topMargin="0"
         bottomMargin="0"
         whenNoDataType="NoPages"
         isTitleNewPage="false"
         isSummaryNewPage="false">
    <property name="ireport.scriptlethandling" value="2" />
    <property name="ireport.encoding" value="UTF-8" />
    <import value="java.util.*" />
    <import value="net.sf.jasperreports.engine.*" />
    <import value="net.sf.jasperreports.engine.data.*" />

    <parameter name="userid" isForPrompting="true" class="java.lang.Integer"/>

    <field name="id" class="java.lang.Integer"/>
    <field name="userid" class="java.lang.String"/>
    <field name="email" class="java.lang.String"/>

        <background>
            <band height="0"  isSplitAllowed="true" >
            </band>
        </background>
        <title>
            <band height="9"  isSplitAllowed="true" >
            </band>
        </title>
        <pageHeader>
            <band height="8"  isSplitAllowed="true" >
            </band>
        </pageHeader>
        <columnHeader>
            <band height="7"  isSplitAllowed="true" >
            </band>
        </columnHeader>
        <detail>
            <band height="57"  isSplitAllowed="true" >
                <rectangle>
                    <reportElement
                        x="5"
                        y="12"
                        width="455"
                        height="24"
                        backcolor="#FF9999"
                        key="rectangle-1"/>
                    <graphicElement stretchType="NoStretch"/>
                </rectangle>
                <staticText>
                    <reportElement
                        x="10"
                        y="15"
                        width="41"
                        height="17"
                        forecolor="#000000"
                        key="staticText-1"/>
                    <box></box>
                    <textElement verticalAlignment="Middle">
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[ID]]></text>
                </staticText>
                <staticText>
                    <reportElement
                        x="58"
                        y="15"
                        width="114"
                        height="17"
                        forecolor="#000000"
                        key="staticText-2"/>
                    <box></box>
                    <textElement verticalAlignment="Middle">
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[User id]]></text>
                </staticText>
                <staticText>
                    <reportElement
                        x="180"
                        y="15"
                        width="114"
                        height="17"
                        forecolor="#000000"
                        key="staticText-3"/>
                    <box></box>
                    <textElement verticalAlignment="Middle">
                        <font size="12"/>
                    </textElement>
                <text><![CDATA[Email]]></text>
                </staticText>
                <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="10"
                        y="39"
                        width="41"
                        height="15"
                        key="textField-1"/>
                    <box></box>
                    <textElement>
                        <font/>
                    </textElement>
                <textFieldExpression   class="java.lang.Integer"><![CDATA[$F{id}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="58"
                        y="39"
                        width="114"
                        height="15"
                        key="textField-2"/>
                    <box></box>
                    <textElement>
                        <font/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$F{userid}]]></textFieldExpression>
                </textField>
                <textField isStretchWithOverflow="false" isBlankWhenNull="false" evaluationTime="Now" hyperlinkType="None"  hyperlinkTarget="Self" >
                    <reportElement
                        x="180"
                        y="39"
                        width="114"
                        height="15"
                        key="textField-3"/>
                    <box></box>
                    <textElement>
                        <font/>
                    </textElement>
                <textFieldExpression   class="java.lang.String"><![CDATA[$F{email}]]></textFieldExpression>
                </textField>
            </band>
        </detail>
</jasperReport>

----------

我创建了三个Java
UserFormatDTO.java

import java.io.Serializable;
import java.util.List;

public class UserFormatDTO implements Serializable {

    private int id;
    private String name;
    private String city;
    private List<UserEmailDTO> emailList;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public List<UserEmailDTO> getEmailList() {
        return emailList;
    }

    public void setEmailList(List<UserEmailDTO> emailList) {
        this.emailList = emailList;
    }

    @Override
    public String toString() {
        return "UserFormatDTO{" + "id=" + id + ", name=" + name + ", city=" + city + '}';
    }
}

UserEmailDTO.java

import java.io.Serializable;

public class UserEmailDTO implements Serializable {
    private int id;
    private int userid;
    private String email;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return "UserEmailDTO{" + "id=" + id + ", userid=" + userid + ", email=" + email + '}';
    }
}

ExportUser.java

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.export.JRPdfExporter;

public class ExportUser {

    private static void setAttachment(String fileName, HashMap hashMap, List<UserFormatDTO> record) {
        String jrxmlReportPath = "Statements/" + fileName + ".jrxml";

        String imgLogo = "img/brand.png";

        hashMap.put("imgLogo", imgLogo);

        byte[] setAttachmentContent = null;

        try {
            JasperReport jasperReport1 = JasperCompileManager.compileReport(jrxmlReportPath);
            JRBeanCollectionDataSource beanDataSource1 = new JRBeanCollectionDataSource(record, true);
            JRDataSource jrDatasource1 = (JRDataSource) beanDataSource1;
            System.out.println("```````````Generating Report``````````");

//            hashMap.put("SUBREPORT_DIR", "Statements/");

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport1, hashMap, jrDatasource1);
            String outFileNamePDF = "UserDetails.pdf";
            JRExporter exporter1 = new JRPdfExporter();
            exporter1.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileNamePDF);
            exporter1.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter1.exportReport();
            System.out.println("Created file: " + outFileNamePDF);

            System.out.println("Done!");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        try {
            List<UserFormatDTO> userFormatDTOs = new ArrayList<UserFormatDTO>();
            List<UserEmailDTO> userEmailDTOs = new ArrayList<UserEmailDTO>();

            UserFormatDTO userFormatDTO1 = new UserFormatDTO();
                userFormatDTO1.setId('1');
                userFormatDTO1.setName("Ram");
                userFormatDTO1.setCity("Pokhara");

                UserEmailDTO userEmailDTO1 = new UserEmailDTO();
                userEmailDTO1.setUserid('1');
                userEmailDTO1.setEmail("firstUser@firstMail");
                userEmailDTOs.add(userEmailDTO1);

                UserEmailDTO userEmailDTO1a = new UserEmailDTO();
                userEmailDTO1a.setId('2');
                userEmailDTO1a.setUserid('1');
                userEmailDTO1a.setEmail("firstUser@secondMail");
                userEmailDTOs.add(userEmailDTO1a);

                userFormatDTO1.setEmailList(userEmailDTOs);

            userFormatDTOs.add(userFormatDTO1);

            UserFormatDTO userFormatDTO2 = new UserFormatDTO();
                userFormatDTO2.setId('2');
                userFormatDTO2.setName("Shyam");
                userFormatDTO2.setCity("Kathmandu");

                userEmailDTOs = new ArrayList<UserEmailDTO>();
                UserEmailDTO userEmailDTO2 = new UserEmailDTO();
                userEmailDTO2.setId('3');
                userEmailDTO2.setUserid('2');
                userEmailDTO2.setEmail("secondUser@firstMail");
                userEmailDTOs.add(userEmailDTO2);

                userFormatDTO2.setEmailList(userEmailDTOs);

            userFormatDTOs.add(userFormatDTO2);

            setAttachment("userReport", new HashMap(), userFormatDTOs);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

为什么会出现错误?

您需要将参数传递为String类型。

子报表参数属性的更改 在此处输入图片说明

如果您仔细查看异常:

net.sf.jasperreports.engine.JRException: Could not load object from location : nullemailReport.jasper

您会在子报表emailReport.jasper名称之前看到null。

看着你的jrxml

<subreportExpression  class="java.lang.String"><![CDATA[$P{SUBREPORT_DIR} + "emailReport.jasper"]]></subreportExpression>

因为未设置参数SUBREPORT_DIR,所以会出现null。

需要将此参数传递到主报告中才能找到子报告。 建议对该参数使用绝对路径而不是相对路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM