如何将任意对象作为参数传递给jasper报告? [英] How can I pass an arbitrary object to jasper report as parameter?

查看:142
本文介绍了如何将任意对象作为参数传递给jasper报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想作为参数传递给我的.jrxml我的域的任意对象,例如Person。

I would like to pass as a parameter to my .jrxml an arbitrary object of my domain, e.g a Person.

InputStream reportFile = MyPage.this.getClass().getResourceAsStream("test.jrxml");
HashMap<String, Person> parameters = new HashMap<String, Person>();
parameters.put("person", new Person("John", "Doe"));
...
JasperReport report = JasperCompileManager.compileReport(reportFile);
JasperPrint print = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
return JasperExportManager.exportReportToPdf(print);

在.jrxml上执行以下操作:

And on the .jrxml do something like:

<?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="test" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
 <property name="ireport.zoom" value="1.0"/>
 <property name="ireport.x" value="0"/>
 <property name="ireport.y" value="0"/>
 <parameter name="PERSON" isForPrompting="false" class="myApp.domain.person"/>
 <background>
  <band splitType="Stretch"/>
 </background>
 <title>
  <band height="20">
       <staticText>
         <reportElement x="180" y="0" width="200" height="20"/>
         <text><![CDATA[$P{PERSON.lastName}]]></text>
       </staticText>
     </band>
 </title>
...

这样的事情可能吗?我在哪里可以找到更复杂的教程,而不仅仅是传递java.lang.String?

Is something like this possible? Where can I find more complex tutorials that show more than just passing a java.lang.String?

谢谢

推荐答案

是的,您可以传递任何Java对象,但是您应该确保在JRXML中导入该对象。

Yes, you can pass any Java object, but you should make sure to import that object in the JRXML.

jasperReport标签。您可以使用标签 import ,例如:

Inside the jasperReport tag. You can use the tag import, like:

 <jasperReport...>
      <import value="org.justfortest.Person">

但是,您可以使用 JRBeanCollectionDataSource 并填充带有对象列表的报告,无需在params映射中存储任意对象。

However, you can use JRBeanCollectionDataSource and populate the report with a list of your object, without needing to store arbitrary objects in the params map.

查看本教程以获取有关 Jasper Reports Bean Collection数据源

Check this tutorial for more info on Jasper Reports Bean Collection Data Source

这篇关于如何将任意对象作为参数传递给jasper报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆