net.sf.jasperreports.engine.JRException:从 bean 检索字段值时出错:First_name

问题描述 投票:0回答:3

我想使用 Jasper Reports 创建 CSV。有很多相关的问题,但没有任何对我有用。下面是我的 Bean 类:

public class DataBean {

    public String First_name;
    public String Last_name;
    public String Designation;
    public String Department;

    public void setfirstName(String First_name) {
        this.First_name = First_name;
    }

    public String getfirstName() {
        return First_name;
    } 

    public void setLastName(String Last_name) {
        this.Last_name = Last_name;
    }
    public String getlastName() {
        return Last_name;
   }
    public void setdesignation(String Designation) {
        this.Designation = Designation;
    }
    public String getdesignation() {
       return Designation;
    }
    public void setDepartment(String Department) {
        this.Department = Department;
    }
    public String getDepartment() {
        return Department;
    }
}

下面是从数据库获取数据并使用 Jasper Report 生成 CSV 的类:

import java.util.HashMap;
import java.util.Map;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.export.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

import com.mysql.jdbc.PreparedStatement;


public class GetResult 
{
    PreparedStatement pst=null;
    DBConnection dbcon=null;
    String First_name;
    String Last_name;
    String Designation;
    String Department;
    net.sf.jasperreports.engine.JasperReport report = null;
    JasperDesign design = null;
    JasperPrint print = null;
    InputStream input = null;
    public void result(){
        try{
            dbcon=new DBConnection();
            input = new FileInputStream(new File("C:/Users/adiuser1/report2.jrxml"));//report2.jasper
           design = JRXmlLoader.load(input);
            ArrayList<DataBean> al2=new ArrayList<DataBean>();
            Map<String, Object> parameters = new HashMap<String, Object>();
            pst = (PreparedStatement) dbcon.conn.prepareStatement("SELECT First_name, Last_name,     Designation, Department FROM emp_data");
            java.sql.ResultSet rs=pst.executeQuery();
            parameters.put("First_name","First_name");
            parameters.put("Last_name","Last_name");
           parameters.put("Designation","Designation");
           parameters.put("Department","Department");
           while(rs.next())
           {
                DataBean db=new DataBean();
                First_name = rs.getString("First_name");
                db.setfirstName(First_name);    
                Last_name = rs.getString("Last_name");
                db.setLastName(Last_name);
                Designation = rs.getString("Designation");
                db.setdesignation(Designation);
                Department = rs.getString("Department");
                db.setDepartment(Department);
                al2.add(db);
            }
            System.out.println("size: "+parameters.size());
            System.out.println("size: "+al2.size());
            report = JasperCompileManager.compileReport(design);
            JRBeanCollectionDataSource masterDS = null;
            masterDS = new JRBeanCollectionDataSource(al2,false);
            print = JasperFillManager.fillReport(report, parameters, masterDS);
            JRCsvExporter csvExporter = new JRCsvExporter();
            csvExporter.setParameter(JRCsvExporterParameter.JASPER_PRINT, print);
            csvExporter.setParameter(JRCsvExporterParameter.OUTPUT_FILE_NAME, "X:/abc.csv");
            csvExporter.setParameter(JRCsvExporterParameter.CHARACTER_ENCODING, "ISO-8859-1");
            csvExporter.exportReport();
       }
        catch(JRException jrException)
        {
            System.out.print(jrException);
            jrException.printStackTrace();
        }
       catch(Exception e)
       {
           System.out.println(e);
      }
      }
   public static void main(String ...s)
   {
        GetResult gr=new GetResult();
       gr.result();
  }
}

下面是我的 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="report2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" isIgnorePagination="true" uuid="204c18b7-de66-4171-b8b8-82a20b837f28">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="0"/>
<subDataset name="sum" uuid="73ad5db5-041e-4e27-aed3-acaf6deec1b8">
    <queryString language="SQL">
        <![CDATA[]]>
    </queryString>
    <field name="First_name" class="java.lang.String"/>
    <field name="Last_name" class="java.lang.String"/>
    <field name="Designation" class="java.lang.String"/>
    <field name="Department" class="java.lang.String"/>
</subDataset>
<field name="First_name" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="Last_name" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="Designation" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<field name="Department" class="java.lang.String">
    <fieldDescription><![CDATA[]]></fieldDescription>
</field>
<background>
    <band splitType="Stretch"/>
</background>
<columnHeader>
    <band height="35" splitType="Stretch">
        <staticText>
            <reportElement uuid="2322e160-4c43-4027-af14-3c69a4d236ed" x="8" y="4" width="100" height="20" backcolor="#003333"/>
            <textElement/>
            <text><![CDATA[First_name]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="a5f84e13-ffee-491f-aa52-602b7b4e9c99" x="149" y="4" width="100" height="20" forecolor="#9999FF" backcolor="#00FFFF"/>
            <textElement/>
            <text><![CDATA[Last_name]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="cd216988-6061-4ef2-8f4d-18e31cabd9e1" x="299" y="4" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Designation]]></text>
        </staticText>
        <staticText>
            <reportElement uuid="22587697-d3b8-4a55-b81d-d380a4e19a7a" x="433" y="4" width="100" height="20"/>
            <textElement/>
            <text><![CDATA[Department]]></text>
        </staticText>
    </band>
</columnHeader>
<detail>
    <band height="26" splitType="Stretch">
        <textField>
            <reportElement uuid="3f6821a6-3aa1-418f-aa68-25b844256276" key="First_name" x="8" y="0" width="100" height="21"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{First_name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="e453bc45-a734-4db6-bba5-19a24252d3ba" x="149" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Last_name}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="5c604a9d-5dba-4b47-9e8c-41de0a29323c" x="299" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Designation}]]></textFieldExpression>
        </textField>
        <textField>
            <reportElement uuid="fa2e53f2-ff71-485d-b9ef-0259ace3eabb" x="433" y="0" width="100" height="20"/>
            <textElement/>
            <textFieldExpression><![CDATA[$F{Department}]]></textFieldExpression>
        </textField>
    </band>
</detail>
<columnFooter>
    <band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
    <band height="54" splitType="Stretch"/>
</pageFooter>
<summary>
    <band height="42" splitType="Stretch"/>
</summary>
</jasperReport>

当我运行

GetResult.java
时,它给出以下错误:

net.sf.jasperreports.engine.JRException: Error retrieving field value from bean : First_namenet.sf.jasperreports.engine.JRException: Error retrieving field value from bean : First_name
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:123)
at net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getFieldValue(JRAbstractBeanDataSource.java:96)
at net.sf.jasperreports.engine.data.JRBeanCollectionDataSource.getFieldValue(JRBeanCollectionDataSource.java:100)
at net.sf.jasperreports.engine.fill.JRFillDataset.setOldValues(JRFillDataset.java:1331)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1232)
at net.sf.jasperreports.engine.fill.JRFillDataset.next(JRFillDataset.java:1208)
at net.sf.jasperreports.engine.fill.JRBaseFiller.next(JRBaseFiller.java:1577)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:149)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:932)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:864)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:969)
at GetResult.result(GetResult.java:78)
at GetResult.main(GetResult.java:99)
Caused by: java.lang.NoSuchMethodException: Unknown property 'First_name'
at org.apache.commons.beanutils.PropertyUtils.getSimpleProperty(PropertyUtils.java:1175)
at org.apache.commons.beanutils.PropertyUtils.getNestedProperty(PropertyUtils.java:772)
at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:801)
at     net.sf.jasperreports.engine.data.JRAbstractBeanDataSource.getBeanProperty(JRAbstractBeanDataSource.java:111)
... 14 more

请帮我解决这个问题。

java jasper-reports javabeans
3个回答
11
投票

将 JRXML 中的字段

$F{First_name}
重命名为
$F{firstName}

更改您的 getter 和 setter 方法以进行对应。喜欢:

String firstName;
public String getFirstName() {
    return firstName;
} 
public void setFirstName(String First_name) {
        this.firstName= First_name;
}

Jasper 寻找尊重 java 约定的相应 getter。


0
投票

只需将报告文件中的字段重命名为以小写字母开头。然后,如果您需要报告上的字段以大写字母开头,您只需在设计纸(静态文本)上更改它即可。 这是我的代码,我认为它与你的类似:

<queryString>
    <![CDATA[]]>
</queryString>
<field name="no" class="java.lang.Integer"/>
<field name="pname" class="java.lang.String"/>
<field name="price" class="java.lang.Double"/>
<field name="qty" class="java.lang.Integer"/>
<variable name="DisPrice" class="java.lang.Double">

//------------------------------------------------------------ --------------------------------

    <band height="33" splitType="Stretch">
        <staticText>
            <reportElement x="0" y="0" width="51" height="16" uuid="f536be7a-1840-45d5-9e9f-4b12989e2a07">
                <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7a4542c6-45f9-4419-8e89-80eb899c9cf6"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
            </reportElement>
            <textElement textAlignment="Center">
                <font isBold="true"/>
            </textElement>
            <text><![CDATA[No]]></text>
        </staticText>
        <staticText>
            <reportElement x="51" y="0" width="49" height="16" uuid="5a327b35-9607-4a80-bd21-0094c8c410b8">
                <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="97cae40e-39c1-4920-84ad-21829e4f3260"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
            </reportElement>
            <textElement textAlignment="Center">
                <font isBold="true"/>
            </textElement>
            <text><![CDATA[Pname]]></text>
        </staticText>
        <staticText>
            <reportElement x="100" y="0" width="49" height="16" uuid="dd5b5d95-f25a-4fa1-99fc-988818bc33d4">
                <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="7e127586-4648-4239-b23c-dbeb38e8024a"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
            </reportElement>
            <textElement textAlignment="Center">
                <font isBold="true"/>
            </textElement>
            <text><![CDATA[Price]]></text>
        </staticText>
        <staticText>
            <reportElement x="149" y="0" width="49" height="16" uuid="6b286509-8c9b-4ed4-add2-ae7654b63842">
                <property name="com.jaspersoft.studio.spreadsheet.connectionID" value="3967222c-bc54-4251-927c-7991b086e3ce"/>
                <property name="com.jaspersoft.studio.unit.height" value="px"/>
            </reportElement>
            <textElement textAlignment="Center">
                <font isBold="true"/>
            </textElement>
            <text><![CDATA[Qty]]></text>
        </staticText>
    </band>

0
投票

需要解决三件事:

  1. 将字段重命名为以小写字母开头,并重命名相应的 getter 和 setter 方法。
  2. 您应该在 Jasper 属性设置中将 java 构建路径更新为 jdk 11(Jasper studio -> 项目 -> 属性 -> Java 构建路径)。
  3. 确保您的实现“net.sf.jasperreports:jasperreports:6.2.0”版本与 Jasper 属性设置中的编译版本匹配(Jasper studio -> 项目 -> 属性 -> Jasper Studio -> 兼容性)。
© www.soinside.com 2019 - 2024. All rights reserved.