在jasper报告

时间:2017-07-25 06:38:52

标签: java jasper-reports javabeans

我必须使用setter getter方法检索Jasper报告中的数据,但它会在

下面给出错误
  

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

我的文件是

jasperInvoice.java

package jasper_project;

  import java.io.File;
  import java.sql.Connection;
  import java.text.DateFormat;
  import java.text.DecimalFormat;
  import java.text.NumberFormat;
  import java.text.SimpleDateFormat;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.HashMap;
  import java.util.List;

  import net.sf.jasperreports.engine.JRException;
  import net.sf.jasperreports.engine.JasperExportManager;
  import net.sf.jasperreports.engine.JasperFillManager;
  import net.sf.jasperreports.engine.JasperPrint;
  import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
  import net.sf.jasperreports.view.JasperViewer;

 public class JasperInvoice {

NumberFormat num_format = new DecimalFormat("#0.00");


 DateFormat dtf = new SimpleDateFormat("yyyy/MM/dd");
 static String view = null;


@SuppressWarnings({"unused", "rawtypes", "unchecked"})
public void generate() {
    try {

        String logPath = new File(".").getAbsolutePath();

        String temp_path = logPath.substring(0, logPath.length() - 1);
        String report = temp_path + "src\\export\\report2.jasper";

        DataSource ds = new DataSource();

        ds.setCompany_name("JettySOft");
        ds.setCompany_address("6480 Sophia st");
        ds.setCompany_zipcode("v5x1s9");
        ds.setCompany_phone_web_mail("info@jettysoft.com");
        ds.setCustomer_name("JITHENDRA");
        ds.setCustomer_address("6480 Sophia st");
        ds.setCustomer_zip("v5x1s9");
        ds.setCustomer_phone("6047008558");
        ds.setCustomer_web_mail("test@mail.com");
        ds.setDate("2017/07/22");
        ds.setInvoice_no("1409");
        ds.setInvoice_type("Delivery");
        ds.setTerms("Terms andnorms");
        ds.setNotes("Remark Test");
        ds.setSignature_name("Veerendra Jetty");
        ds.setSignature_title("JettySoft LTD");
        ds.setFooter("Footer");

        ds.setSubtotal(num_format.format(10.99));

        ds.setGst(num_format.format(5.0));
        ds.setPst(num_format.format(5.0));
        double sgst = 2 / 2;
        ds.setIgst(num_format.format(1.0));
        ds.setCgst(num_format.format(2.0));
        ds.setSgst(num_format.format(2.0));

        ds.setTotalall(num_format.format(12.99));
        ds.setDeposit(num_format.format(2.99));
        ds.setBalance(num_format.format(10.00));

        List<Item> items = new ArrayList<>();

        for (int t = 0; t < 10; t++) {
            items.add(new Item("Test D"+t, "2"+t, "25" + t,"50" + t));               
        }           
        ds.setItems(items);

        JRBeanCollectionDataSource jrbcds = new JRBeanCollectionDataSource(Arrays.asList(ds));
        Connection connection = null;

        HashMap hm = new HashMap();

        hm.put("bckColor", "GREEN");
        hm.put("logo_path", "images/logo.png"); //place some image here


        // Generate jasper print
        JasperPrint jprint = JasperFillManager.fillReport(report, hm, jrbcds);//replace with your file
        // JREmptyDataSource()

        // Export pdf file
        String exportin = temp_path + "src\\Invoices\\invoice1.pdf";
        JasperExportManager.exportReportToPdfFile(jprint, exportin);
        // this can be skipped - just the preview

        JasperViewer.viewReport(jprint, false);


    } catch (JRException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public static void main(String args[]) {
    JasperInvoice in = new JasperInvoice();
    in.generate();
}

}

DataSource.java

package jasper_project;

import java.util.List;

public class DataSource {

List<Item> items;
String company_name;
String customer_address;
String company_zipcode;
String company_phone_web_mail;
String company_address;
String customer_name;
String customer_zip;
String customer_phone;
String customer_web_mail;
String date;
String invoice_no;
String invoice_type;
String terms;
String notes;
String signature_name;
String signature_title;
String footer;
String subtotal;
String gst;
String pst;
String igst;
String cgst;
String sgst;
String totalall;
String deposit;
String balance;



public DataSource() {
}

public List<Item> getItems() {
    return items;
}

public void setItems(List<Item> items) {
    this.items = items;
}


public String getCompany_name() {
    return company_name;
}

public void setCompany_name(String company_name) {
    this.company_name = company_name;
}

public String getCustomer_address() {
    return customer_address;
}

public void setCustomer_address(String customer_address) {
    this.customer_address = customer_address;
}

public String getCompany_zipcode() {
    return company_zipcode;
}

public void setCompany_zipcode(String company_zipcode) {
    this.company_zipcode = company_zipcode;
}

public String getCompany_phone_web_mail() {
    return company_phone_web_mail;
}

public void setCompany_phone_web_mail(String company_phone_web_mail) {
    this.company_phone_web_mail = company_phone_web_mail;
}

public String getCompany_address() {
    return company_address;
}

public void setCompany_address(String company_address) {
    this.company_address = company_address;
}

public String getCustomer_name() {
    return customer_name;
}

public void setCustomer_name(String customer_name) {
    this.customer_name = customer_name;
}

public String getCustomer_zip() {
    return customer_zip;
}

public void setCustomer_zip(String customer_zip) {
    this.customer_zip = customer_zip;
}

public String getCustomer_phone() {
    return customer_phone;
}

public void setCustomer_phone(String customer_phone) {
    this.customer_phone = customer_phone;
}

public String getCustomer_web_mail() {
    return customer_web_mail;
}

public void setCustomer_web_mail(String customer_web_mail) {
    this.customer_web_mail = customer_web_mail;
}

public String getDate() {
    return date;
}

public void setDate(String date) {
    this.date = date;
}

public String getInvoice_no() {
    return invoice_no;
}

public void setInvoice_no(String invoice_no) {
    this.invoice_no = invoice_no;
}

public String getInvoice_type() {
    return invoice_type;
}

public void setInvoice_type(String invoice_type) {
    this.invoice_type = invoice_type;
}

public String getTerms() {
    return terms;
}

public void setTerms(String terms) {
    this.terms = terms;
}

public String getSignature_name() {
    return signature_name;
}

public void setSignature_name(String signature_name) {
    this.signature_name = signature_name;
}

public String getSignature_title() {
    return signature_title;
}

public void setSignature_title(String signature_title) {
    this.signature_title = signature_title;
}

public String getFooter() {
    return footer;
}

public void setFooter(String footer) {
    this.footer = footer;
}

public String getSubtotal() {
    return subtotal;
}

public void setSubtotal(String subtotal) {
    this.subtotal = subtotal;
}

public String getGst() {
    return gst;
}

public void setGst(String gst) {
    this.gst = gst;
}

public String getPst() {
    return pst;
}

public void setPst(String pst) {
    this.pst = pst;
}

public String getIgst() {
    return igst;
}

public void setIgst(String igst) {
    this.igst = igst;
}

public String getCgst() {
    return cgst;
}

public void setCgst(String cgst) {
    this.cgst = cgst;
}

public String getSgst() {
    return sgst;
}

public void setSgst(String sgst) {
    this.sgst = sgst;
}

public String getTotalall() {
    return totalall;
}

public void setTotalall(String totalall) {
    this.totalall = totalall;
}

public String getNotes() {
    return notes;
}

public void setNotes(String notes) {
    this.notes = notes;
}

public String getDeposit() {
    return deposit;
}

public void setDeposit(String deposit) {
    this.deposit = deposit;
}

public String getBalance() {
    return balance;
}

public void setBalance(String balance) {
    this.balance = balance;
}
}

Item.java

package jasper_project;

public class Item {

private String itemdescription;
private String quantity;
private String price;
private String total;



public Item(String itemdescription, String quantity, String price, String total) {

    this.itemdescription = itemdescription;
    this.quantity = quantity;
    this.price = price;
    this.total = total;
}



public String getItemdescription() {
    return itemdescription;
}

public void setItemdescription(String itemdescription) {
    this.itemdescription = itemdescription;
}

public String getQuantity() {
    return quantity;
}

public void setQuantity(String quantity) {
    this.quantity = quantity;
}

public String getPrice() {
    return price;
}

public void setPrice(String price) {
    this.price = price;
}

public String getTotal() {
    return total;
}

public void setTotal(String total) {
    this.total = total;
}
}

请帮助我解决这个问题我是jasper报告的新成员

jrxml字段解析

<subDataset name="tableDataset" uuid="f13e6d36-5148-4ecc-bbe3-3035def80980">
    <queryString>
        <![CDATA[]]>
    </queryString>
    <field name="itemdescription" class="java.lang.String"/>
    <field name="quantity" class="java.lang.String"/>
    <field name="price" class="java.lang.String"/>
    <field name="total" class="java.lang.String"/>
    <field name="itemtitle" class="java.lang.String"/>
</subDataset>

jrxml表达

<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="tableDataset" uuid="4b4d7d62-c8f1-415e-be8a-a42cea194f33">
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{items})]]></dataSourceExpression>
                </datasetRun>
                <jr:column width="200" uuid="292709f4-133d-4a94-804e-1bca54243aaf">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                    <jr:columnHeader style="Table 1_TH" height="30" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="200" height="30" forecolor="#FFFFFF" backcolor="#FFFFFF" uuid="333e63a6-f5bc-4c78-b036-79e241d3594d"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="14" isBold="true"/>
                            </textElement>
                            <text><![CDATA[Item Description]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="200" height="15" uuid="db6ddde5-a058-4a6f-96f5-9596bb3836b7"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="10" isBold="true"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{itemtitle}]]></textFieldExpression>
                        </textField>
                        <textField>
                            <reportElement x="0" y="15" width="200" height="15" forecolor="#666666" uuid="8f371a2c-5a94-4a24-ba55-57e31c868641"/>
                            <textElement textAlignment="Center">
                                <font fontName="Calisto MT"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{itemdescription}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="100" uuid="3c3659d9-7059-46c6-aa21-7dd80d7ca1ff">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                    <jr:columnHeader style="Table 1_TH" height="30" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="100" height="30" forecolor="#FFFFFF" backcolor="#FFFFFF" uuid="1b3f3548-78b0-40d4-92de-ee18407102a8"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="14" isBold="true"/>
                            </textElement>
                            <text><![CDATA[Quantity
     ]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="9feca163-f403-41bd-8b14-a69aae0f6dc8"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="12" isBold="false"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{quantity}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="100" uuid="3a1fe68c-3eb4-4d8d-b7b5-f49aa54d9bee">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/>
                    <jr:columnHeader style="Table 1_TH" height="30" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="100" height="30" forecolor="#FFFFFF" backcolor="#FFFFFF" uuid="dc36e843-e1a5-4bca-a4b6-87af245d7b62"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="14" isBold="true"/>
                            </textElement>
                            <text><![CDATA[Price]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="b41298ed-678b-4f43-a1b9-e3f08696f265"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="12" isBold="false"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{price}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="100" uuid="6612d4cc-c959-4c31-bae9-9d09b2e72c03">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column4"/>
                    <jr:columnHeader style="Table 1_TH" height="30" rowSpan="1">
                        <staticText>
                            <reportElement x="0" y="0" width="100" height="30" forecolor="#FFFFFF" uuid="43b5adff-338e-4808-a8b0-f78d27bd2c97"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="16" isBold="true"/>
                            </textElement>
                            <text><![CDATA[Total]]></text>
                        </staticText>
                    </jr:columnHeader>
                    <jr:detailCell style="Table 1_TD" height="30" rowSpan="1">
                        <textField>
                            <reportElement x="0" y="0" width="100" height="30" uuid="d1f7d2a8-289e-4756-b61d-07828b46eb1a"/>
                            <textElement textAlignment="Center" verticalAlignment="Middle">
                                <font fontName="Calisto MT" size="12"/>
                            </textElement>
                            <textFieldExpression><![CDATA[$F{total}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>

0 个答案:

没有答案