我正在尝试从jasperReport库生成PDF。
我尝试了一些代码,但它只生成一个带有一些垃圾值或测试字符串的PDF,我已经设置了测试。 我的PDF看起来像这样
null null(ID:null)null
这是Demo for 共1页
这是我的购买类代码
package org.chillies.database;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import org.hibernate.annotations.Index;
@Entity
public class Purchase {
private int id;
private String name;
private String companyName;
private double discount;
private String remark;
private List<SupplyOrder> supplyOrder = new ArrayList<SupplyOrder>();
private AddressDetails addressdetails;
private ContactDetails contactdetails;
public static final String KeypurchaseTable = "purchase";
private static final String Keyaddressjoincolumnname="addressIdfk";
private static final String Keycontactjoincolumnname="contactIdfk";
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name=Keyaddressjoincolumnname)
public AddressDetails getAddressdetails() {
return addressdetails;
}
public void setAddressdetails(AddressDetails addressdetails) {
this.addressdetails = addressdetails;
}
@OneToOne(cascade=CascadeType.ALL,fetch=FetchType.EAGER)
@JoinColumn(name=Keycontactjoincolumnname)
public ContactDetails getContactdetails() {
return contactdetails;
}
public void setContactdetails(ContactDetails contactdetails) {
this.contactdetails = contactdetails;
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public int getId() {
return id;
}
public void setId(int purchaseid) {
this.id = purchaseid;
}
private static final String KeyinndexPurchaseName="indexPurchaseName";
@Index(name = KeyinndexPurchaseName)
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public double getDiscount() {
return discount;
}
public void setDiscount(double discount) {
this.discount = discount;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@OneToMany(targetEntity = SupplyOrder.class, mappedBy = KeypurchaseTable, cascade = CascadeType.ALL)
public List<SupplyOrder> getSupplyOrder() {
return supplyOrder;
}
public void setSupplyOrder(List<SupplyOrder> supplyOrder) {
this.supplyOrder = supplyOrder;
}
}
生成PDF的类
package org.chillies.reports;
import java.io.File;
import java.util.HashMap;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
public class JasperReportsIntro {
public static void main(String[] args) {
JasperReport jasperReport;
JasperPrint jasperPrint;
try {
jasperReport = JasperCompileManager.compileReport("jrxmls"
+ File.separator + "PurchaseReport.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport,new HashMap(), new JREmptyDataSource());
JasperExportManager.exportReportToPdfFile(jasperPrint,
"Genratedreports/simple_report.pdf");
System.out.println("Completed");
} catch (JRException e) {
e.printStackTrace();
}
}
}
我的JRXML文件PurchaseReport.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="PurchaseesReport" pageWidth="595" pageHeight="842" whenNoDataType="AllSectionsNoDetail" columnWidth="515" leftMargin="40" rightMargin="40" topMargin="50" bottomMargin="50" uuid="f050e3db-0847-4912-9c16-d6b157484813">
<import value="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/>
<style name="Sans_Normal" isDefault="true" fontName="Calisto MT" fontSize="12"/>
<style name="Sans_Normal_Small" fontName="Calisto MT" fontSize="9"/>
<style name="Sans_Bold" fontName="Calisto MT" fontSize="12" isBold="true"/>
<style name="Sans_Italic" fontName="Calisto MT" fontSize="12" isItalic="true"/>
<parameter name="ReportTitle" class="java.lang.String"/>
<queryString language="hql">
<![CDATA[FROM Purchase]]>
</queryString>
<field name="id" class="java.lang.Integer">
<fieldDescription><![CDATA[id]]></fieldDescription>
</field>
<field name="discount" class="java.lang.Double">
<fieldDescription><![CDATA[discount]]></fieldDescription>
</field>
<field name="name" class="java.lang.String">
<fieldDescription><![CDATA[name]]></fieldDescription>
</field>
<field name="remark" class="java.lang.String">
<fieldDescription><![CDATA[remark]]></fieldDescription>
</field>
<group name="PurchaseGroup">
<groupExpression><![CDATA[$F{id}]]></groupExpression>
<groupHeader>
<band height="25">
<frame>
<reportElement mode="Opaque" x="0" y="0" width="375" height="21" backcolor="#E0E0E0" uuid="a143642b-fbed-4962-bfbf-c9d9ecc24879"/>
<textField isStretchWithOverflow="true">
<reportElement x="5" y="3" width="160" height="15" uuid="fdadafcd-6622-4d05-89f8-31eeb46e68f9"/>
<textFieldExpression><![CDATA[$F{name} + " " + $F{remark} + " (ID: " + $F{id} + ")"]]></textFieldExpression>
</textField>
<textField isStretchWithOverflow="true">
<reportElement x="210" y="3" width="160" height="15" uuid="8e21ca6a-7eb9-4deb-8b36-9df9db27314c"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$F{discount}]]></textFieldExpression>
</textField>
</frame>
</band>
</groupHeader>
<groupFooter>
<band height="25">
<line>
<reportElement x="20" y="0" width="475" height="1" forecolor="#808080" uuid="89aa8875-ef41-4ade-8143-b12e79955216"/>
<graphicElement>
<pen lineWidth="0.5"/>
</graphicElement>
</line>
<staticText>
<reportElement style="Sans_Normal_Small" x="20" y="2" width="70" height="15" uuid="d248a006-9289-4a01-909d-81608bbdd09b"/>
<text><![CDATA[Total]]></text>
</staticText>
<textField>
<reportElement style="Sans_Normal_Small" x="100" y="2" width="50" height="15" uuid="c4c43f7e-2ce3-4070-818b-e64bb6c27977"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA[$V{PurchaseGroup_COUNT}]]></textFieldExpression>
</textField>
</band>
</groupFooter>
</group>
<title>
<band height="50">
<line>
<reportElement x="0" y="0" width="515" height="1" forecolor="#000000" uuid="6a96d2fc-8ad1-4022-91e6-fcc11f3380f5"/>
</line>
<textField isBlankWhenNull="true">
<reportElement style="Sans_Normal" x="0" y="10" width="515" height="30" uuid="876bd00a-1cd0-485e-8e71-20c402f03048"/>
<textElement textAlignment="Center">
<font size="22"/>
<paragraph lineSpacing="Single"/>
</textElement>
<textFieldExpression><![CDATA[$P{ReportTitle}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="13">
<staticText>
<reportElement style="Sans_Normal_Small" isPrintRepeatedValues="false" x="20" y="0" width="70" height="12" isPrintInFirstWholeBand="true" printWhenGroupChanges="PurchaseGroup" uuid="8d08fcca-f5d3-4c5b-baa0-e5ab70628047"/>
<text><![CDATA[This is Demo for Purchase List]]></text>
</staticText>
</band>
</detail>
<pageFooter>
<band height="40">
<printWhenExpression><![CDATA[$P{IS_IGNORE_PAGINATION}.booleanValue()?Boolean.FALSE:Boolean.TRUE]]></printWhenExpression>
<line>
<reportElement x="0" y="10" width="515" height="1" uuid="7c3195dc-606d-4287-a159-acf9af1e1063"/>
</line>
<textField>
<reportElement x="200" y="20" width="80" height="15" uuid="dba281f2-0790-48c4-a5d8-0a71cfc4c842"/>
<textElement textAlignment="Right"/>
<textFieldExpression><![CDATA["Page " + String.valueOf($V{PAGE_NUMBER}) + " of"]]></textFieldExpression>
</textField>
<textField evaluationTime="Report">
<reportElement x="280" y="20" width="75" height="15" uuid="e6b4b6f0-e454-4c46-ad41-765e42ea01e8"/>
<textElement textAlignment="Left"/>
<textFieldExpression><![CDATA[" " + String.valueOf($V{PAGE_NUMBER})]]></textFieldExpression>
</textField>
</band>
</pageFooter>
答案 0 :(得分:0)
试试这段代码:
package org.chillies.reports;
import java.io.File;
import java.util.HashMap;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
public class JasperReportsIntro {
public static void main(String[] args) {
Purchase purchase = new Purchase();
purchase.setName("Vivek");
purchase.setRemark("This is remark");
purchase.setDiscount(10.0);
purchase.setId(20);
ArrayList<Purchase> list = new ArrayList<Purchase>();
list.add(purchase);
JRDataSource beanCollectionDataSource = new JRBeanCollectionDataSource(list);
JasperReport jasperReport;
JasperPrint jasperPrint;
try {
jasperReport = JasperCompileManager.compileReport("jrxmls"
+ File.separator + "PurchaseReport.jrxml");
jasperPrint = JasperFillManager.fillReport(jasperReport, new HashMap(), beanCollectionDataSource);
JasperExportManager.exportReportToPdfFile(jasperPrint,
"Genratedreports/simple_report.pdf");
System.out.println("Completed");
} catch (JRException e) {
e.printStackTrace();
}
}
}
享受。 :)