简体   繁体   English

使用JasperReports API生成报告时获取空值

[英]Getting null values when generating report using JasperReports API

I've designed my report using iReport and compiled it in Java . 我已经使用iReport设计了报告,并使用Java对其进行了编译。

I'm getting null values when I generate report in PDF format via Java . 通过Java生成PDF格式的报告时,出现了空值。

I get Key is null in pie dataset. 我在派数据集中得到Key为空。 error and the output PDF or html has null values. 错误,并且输出的PDFhtml具有值。

My code: 我的代码:

public class PdfFromJasperFile {

    public static void main(String[] args) throws JRException, IOException {

        JasperReport jasperReport;
        JasperPrint jasperPrint;
        Map param = new HashMap();
        try {
            String sourceFileName = "C:/Users/Sadagopan/Documents/report3.jrxml";

            jasperReport = JasperCompileManager.compileReport(sourceFileName);
            jasperPrint = JasperFillManager.fillReport(jasperReport, param,
                    new JREmptyDataSource());
            JasperExportManager.exportReportToHtmlFile(jasperPrint, 
                    "C:/Users/Sadagopan/Documents/sample.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

My JRXML file: 我的JRXML文件:

<?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="report3" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="3d69a5d5-0c1e-4b1f-9424-513fa329f402">
    <queryString>
        <![CDATA[Select * from person where name ='sadagopan']]>
    </queryString>
    <field name="id" class="java.lang.Long">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="name" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="address" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="phonenumber" class="java.lang.String">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="salary" class="java.math.BigDecimal">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <field name="pension" class="java.math.BigDecimal">
        <fieldDescription><![CDATA[]]></fieldDescription>
    </field>
    <group name="id">
        <groupExpression><![CDATA[$F{id}]]></groupExpression>
    </group>
    <group name="name">
        <groupExpression><![CDATA[$F{name}]]></groupExpression>
    </group>
    <group name="phonenumber">
        <groupExpression><![CDATA[$F{phonenumber}]]></groupExpression>
    </group>
    <group name="salary">
        <groupExpression><![CDATA[$F{salary}]]></groupExpression>
    </group>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="60" splitType="Stretch">
            <staticText>
                <reportElement uuid="a006c204-9feb-4302-90b1-fe5bdddbe3d8" x="89" y="11" width="384" height="49"/>
                <textElement>
                    <font size="36"/>
                </textElement>
                <text><![CDATA[Person Salary report]]></text>
            </staticText>
        </band>
    </title>
    <pageHeader>
        <band height="27" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="25" splitType="Stretch">
            <staticText>
                <reportElement uuid="b74a5413-2dee-4cd5-ac06-e73a022343bc" x="0" y="5" width="48" height="20"/>
                <textElement/>
                <text><![CDATA[ID   ]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="9df2c923-e5b6-4bb0-95f0-5c437495ea53" x="63" y="5" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[Name]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="b054ca79-f62e-4c80-817f-270e9ee4839a" x="163" y="5" width="176" height="20"/>
                <textElement/>
                <text><![CDATA[Address]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="e49b408e-8c6f-4aea-af7b-15ab31eb4643" x="329" y="5" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[Phonenumber]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="4bf8ce1f-5284-4642-a45a-90aeff267abd" x="429" y="5" width="59" height="20"/>
                <textElement/>
                <text><![CDATA[salary]]></text>
            </staticText>
            <staticText>
                <reportElement uuid="f99f4ce6-d8df-41b6-b1cb-8a5ef17b46c3" x="488" y="5" width="100" height="20"/>
                <textElement/>
                <text><![CDATA[pension]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="53" splitType="Stretch">
            <textField>
                <reportElement uuid="e91cc334-4623-42a2-bc78-e6c248606f65" x="0" y="0" width="48" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="3b8498fb-8560-4178-86bf-183ab6855d49" x="63" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="78c2be84-eebd-4465-b8c2-53d201e3246a" x="163" y="0" width="166" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{address}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="813442d3-a459-4fc9-83fb-ac170bf5a9a8" x="329" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{phonenumber}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="37d739d1-f413-465f-aecc-6bc2a3b8ff72" x="429" y="0" width="59" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{salary}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="99c32bab-b3d5-4ade-bb47-b3858ce6e2d6" x="488" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{pension}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
    <columnFooter>
        <band height="236" splitType="Stretch">
            <pieChart>
                <chart>
                    <reportElement uuid="e214bf8f-78d8-424b-a617-4b417385a9e5" x="48" y="21" width="440" height="215"/>
                    <chartTitle/>
                    <chartSubtitle/>
                    <chartLegend/>
                </chart>
                <pieDataset>
                    <keyExpression><![CDATA[$F{id}]]></keyExpression>
                    <valueExpression><![CDATA[$F{salary}]]></valueExpression>
                </pieDataset>
                <piePlot>
                    <plot/>
                    <itemLabel/>
                </piePlot>
            </pieChart>
        </band>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

Its because you are passing JREmptyDataSource() to your report that's why $F{id} is null 这是因为您JREmptyDataSource()传递给报表,因此$F{id}为null

Here are the possible datasources that you can pass to your report. 这是可以传递给报告的可能的数据源

The working code for anyone reference 工作代码供任何人参考

`public class TestJasper { `公共类TestJasper {

public static void main(String[] args) {

     String reportPath = "C:/Users/Sadagopan/Documents/report3.jasper";
     Map<String, Object> params = new HashMap<String, Object>();
     Connection connection;

    try {

       // JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
          Class.forName("com.mysql.jdbc.Driver");
          connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/person","root","password");

        System.out.println("Filling report...");
        JasperPrint jasperPrint = JasperFillManager.fillReport(reportPath, params, connection);
        JasperExportManager.exportReportToHtmlFile(jasperPrint, "C:/Users/Sadagopan/Documents/sample.html"); 
        JasperViewer.viewReport(jasperPrint, false);
        connection.close();
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }

    }
}

' '

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

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