Java (JasperReports)使用JRBeanCollection作为数据源,并在jrxml中处理它
因此,我从数据库中获取一个DAO列表,然后将JRBeanCollectionDataSource传递给fill方法。这可以工作(生成一个空的PDF)Java (JasperReports)使用JRBeanCollection作为数据源,并在jrxml中处理它,java,jasper-reports,Java,Jasper Reports,因此,我从数据库中获取一个DAO列表,然后将JRBeanCollectionDataSource传递给fill方法。这可以工作(生成一个空的PDF) public void reportTest(){ AwardDAO AwardDAO=新的AwardDAOImpl(); 列表奖励=空; 试一试{ awards=awardDAO.getAllAwards(); JRBeanCollectionDataSource ds=新的JRBeanCollectionDataSource(奖励); 贾斯佩雷
public void reportTest(){
AwardDAO AwardDAO=新的AwardDAOImpl();
列表奖励=空;
试一试{
awards=awardDAO.getAllAwards();
JRBeanCollectionDataSource ds=新的JRBeanCollectionDataSource(奖励);
贾斯佩雷波特贾斯佩雷波特=
编译报告(“src/main/resources/hellojasper.jrxml”);
映射参数=新的HashMap();
参数。put(“标题”、“奖励报告”);
茉莉花=
fillReport(jasperReport,parameters,ds);
JasperExportManager.exportReportToPdfFile(
jasperPrint,“Awards.pdf”);
}
捕获(例外e){
e、 printStackTrace();
}
}
我不知道如何处理.jrxml文件中的信息。我已经找了一段时间了,但我只是更加困惑了。
我想显示一个简单的表。
谢谢 您必须在jrxml中声明字段,并在报告的textField表达式中使用该字段。就这些。
参见示例
另外,您确定您的应用程序中的reportTest.class知道路径“src/main/resources/hellojasper.jrxml”吗 确保您的奖励类中要在PDF文件中显示的每个字段都有getter和setter 例如,如果要显示具有以下字段的产品类:
<field name="productName" class="java.lang.String">
<field name="quantity" class="java.lang.String">
<field name="unitPrice" class="java.lang.String">
<field name="total" class="java.lang.String">
然后我可以创建一个JRBeanCollectionDataSource:
List<Product> products = new ArrayList<Product>();
// Code to fill products
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(products);
List products=new ArrayList();
//填充产品的代码
JRBeanCollectionDataSource ds=新的JRBeanCollectionDataSource(产品);
public class Product {
private String productName;
private String quantity;
private String unitPrice;
private String total;
public void setProductName(String productName){
this.productName = productName;
}
public String getProductName(){return productName;}
public void setQuantity(String quantity){
this.quantity = quantity;
}
public String getQuantity(){return quantity;}
public void setUnitPrice(String unitPrice){
this.unitPrice = unitPrice;
}
public String getUnitPrice(){return unitPrice;}
public void setTotal(String total){
this.total = total;
}
public String getTotal(){return total;}
}
List<Product> products = new ArrayList<Product>();
// Code to fill products
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(products);