iReport 笔记(六) 使用子报表(使用JavaBean集合作为数据源)
2008-06-19 11:54
483 查看
p {
font-family: "宋体";
font-size: 12px;
font-style: normal;
line-height: 18px;
font-weight: normal;
text-indent: 24px;
}
h1 {
font-size: 36px;
font-style: normal;
font-weight: bold;
text-decoration: underline;
color: #990000;
text-align: center;
}
h2 {
font-size: 24px;
font-weight: bold;
color: #9900FF;
}
h3 {
font-size: 18px;
font-weight: bold;
color: #000099;
}
a {
color: #660033;
text-decoration: none;
}
img {
vertical-align: middle;
}
ul {
font-family: "宋体";
font-size: 12px;
line-height: 18px;
}
PRE {
font-size: 12px;
padding: 5px;
border-style: solid;
border-width: 1px;
border-color: #CCCCCC;
background-color: #F0F0F0;
}
table {
font-size: 12px;
border-collapse: collapse;
}
tr {
background-color: inherit;
color: black;
padding: 5px;
text-align: left;
border: 1px solid #999999;
}
th {
text-align: center;
background-color: #cccccc;
color: black;
font-weight: bolder;
padding: 5px;
border: 1px solid #999999;
}
td {
border: 1px solid #999999;
padding: 5px;
}
ol {
font-family: "宋体";
font-size: 12px;
line-height: 18px;
}
h4 {
font-size: 16px;
}
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_01.jpg)
ProvinceBean.java
CityBean.java
第二步 创建Servlet类,如下所示
如果有了以前例子的基础,这个例子应该不难看懂,需要注意的是下面这4行代码,这是新出现的特性:
这是因为,在使用子报表时,需要一个SUBREPORT_DIR变量,指示子报表所在的路径,在下面创建子报表的相关部分会再次进行描述,请注意对比。我在以前的例子中,调用JasperFillManager.fillReport()方法时第二个参数均为null,实际上,这个参数可传递到jasper report中的Parameters项中。
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_02.jpg)
其Connection设置如下图所示:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_03.jpg)
其DataSource设置如下图所示
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_04.jpg)
第二步,创建子报表,可使用工具栏的“SubReport”图标直接创建,使用默认的名称,并确定好子报表的位置,利用系统的wizard一步一步设置,注意在第2步设置”Connection/Datasource ”时最好选择”no connection or datasource”)
子报表我们使用CityBean做为数据源,设置方法参考上一步
第三步,在父报表中设置子报表控件的属性,在SubReport面板中设置其“Connection/Data Source Expression”为“new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})”,如下图所示:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_05.jpg)
第四步,编译,将编译生成的.jasper文件放置到项目的相应目录中
注: 如需本例源代码, 请留言
font-family: "宋体";
font-size: 12px;
font-style: normal;
line-height: 18px;
font-weight: normal;
text-indent: 24px;
}
h1 {
font-size: 36px;
font-style: normal;
font-weight: bold;
text-decoration: underline;
color: #990000;
text-align: center;
}
h2 {
font-size: 24px;
font-weight: bold;
color: #9900FF;
}
h3 {
font-size: 18px;
font-weight: bold;
color: #000099;
}
a {
color: #660033;
text-decoration: none;
}
img {
vertical-align: middle;
}
ul {
font-family: "宋体";
font-size: 12px;
line-height: 18px;
}
PRE {
font-size: 12px;
padding: 5px;
border-style: solid;
border-width: 1px;
border-color: #CCCCCC;
background-color: #F0F0F0;
}
table {
font-size: 12px;
border-collapse: collapse;
}
tr {
background-color: inherit;
color: black;
padding: 5px;
text-align: left;
border: 1px solid #999999;
}
th {
text-align: center;
background-color: #cccccc;
color: black;
font-weight: bolder;
padding: 5px;
border: 1px solid #999999;
}
td {
border: 1px solid #999999;
padding: 5px;
}
ol {
font-family: "宋体";
font-size: 12px;
line-height: 18px;
}
h4 {
font-size: 16px;
}
6 子报表
在本例中,我使用子报表将生成如下样式的报表![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_01.jpg)
6.1 程序部分
第一步,创建与之相关的Bean类,如下所示:ProvinceBean.java
package lld.test.ireport; import java.util.ArrayList; public class ProvinceBean { private String provinceName; private ArrayList<CityBean> cities; public String getProvinceName() { return provinceName; } public void setProvinceName(String provinceName) { this.provinceName = provinceName; } public ArrayList<CityBean> getCities() { return cities; } public void setCities(ArrayList<CityBean> cities) { this.cities = cities; } }
CityBean.java
package lld.test.ireport; public class CityBean { private String cityName; public String getCityName() { return cityName; } public void setCityName(String cityName) { this.cityName = cityName; } }
第二步 创建Servlet类,如下所示
package lld.test.ireport; import java.io.IOException; import java.io.OutputStream; import java.net.URLEncoder; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRDataSource; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.util.JRLoader; public class ChildReportServlet extends HttpServlet { private static final long serialVersionUID = -1233414483047719876L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { try { String root_path = this.getServletContext().getRealPath("/"); root_path = root_path.replace('//', '/'); String reportFilePath = root_path + "WEB-INF/classes/lld/test/ireport/child_report_jbs_parent.jasper"; JRDataSource dataSource = this.createDataSource(); Map<String, String> parameters = new HashMap<String, String>(); parameters.put("SUBREPORT_DIR", root_path + "WEB-INF/classes/lld/test/ireport/"); JasperReport report = (JasperReport)JRLoader.loadObject(reportFilePath); JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, dataSource); OutputStream ouputStream = resp.getOutputStream(); resp.setContentType("application/pdf"); resp.setCharacterEncoding("UTF-8"); resp.setHeader("Content-Disposition", "attachment; filename=/"" + URLEncoder.encode("PDF报表", "UTF-8") + ".pdf/""); // 使用JRPdfExproter导出器导出pdf JRPdfExporter exporter = new JRPdfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, ouputStream); exporter.exportReport(); ouputStream.close(); }catch(Exception ex) { ex.printStackTrace(); } } private JRDataSource createDataSource() { //生成测试数据 ArrayList<ProvinceBean> provinces = new ArrayList<ProvinceBean>(); ProvinceBean province = new ProvinceBean(); province.setProvinceName("山东"); ArrayList<CityBean> cities = new ArrayList<CityBean>(); CityBean city = new CityBean(); city.setCityName("济南"); cities.add(city); city = new CityBean(); city.setCityName("青岛"); cities.add(city); city = new CityBean(); city.setCityName("潍坊"); cities.add(city); province.setCities(cities); provinces.add(province); province = new ProvinceBean(); province.setProvinceName("江苏"); cities = new ArrayList<CityBean>(); city = new CityBean(); city.setCityName("南京"); cities.add(city); city = new CityBean(); city.setCityName("无锡"); cities.add(city); city = new CityBean(); city.setCityName("苏州"); cities.add(city); province.setCities(cities); provinces.add(province); return new JRBeanCollectionDataSource(provinces); } }
如果有了以前例子的基础,这个例子应该不难看懂,需要注意的是下面这4行代码,这是新出现的特性:
Map<String, String> parameters = new HashMap<String, String>(); parameters.put("SUBREPORT_DIR", root_path + "WEB-INF/classes/lld/test/ireport/"); JasperReport report = (JasperReport)JRLoader.loadObject(reportFilePath); JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, dataSource);
这是因为,在使用子报表时,需要一个SUBREPORT_DIR变量,指示子报表所在的路径,在下面创建子报表的相关部分会再次进行描述,请注意对比。我在以前的例子中,调用JasperFillManager.fillReport()方法时第二个参数均为null,实际上,这个参数可传递到jasper report中的Parameters项中。
6.2 创建报表
第一步,创建主报表,如下图所示![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_02.jpg)
其Connection设置如下图所示:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_03.jpg)
其DataSource设置如下图所示
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_04.jpg)
第二步,创建子报表,可使用工具栏的“SubReport”图标直接创建,使用默认的名称,并确定好子报表的位置,利用系统的wizard一步一步设置,注意在第2步设置”Connection/Datasource ”时最好选择”no connection or datasource”)
子报表我们使用CityBean做为数据源,设置方法参考上一步
第三步,在父报表中设置子报表控件的属性,在SubReport面板中设置其“Connection/Data Source Expression”为“new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})”,如下图所示:
![](http://p.blog.csdn.net/images/p_blog_csdn_net/lldwolf/ireport_subreport_05.jpg)
第四步,编译,将编译生成的.jasper文件放置到项目的相应目录中
注: 如需本例源代码, 请留言
相关文章推荐
- iReport 笔记(六) 使用子报表(使用JavaBean集合作为数据源)
- iReport 笔记(六) 使用子报表(使用JavaBean集合作为数据源) 【转】
- Ireport使用子报表(使用JavaBean集合作为数据源)
- iReport 笔记(三) 使用JavaBean集合作为数据源
- iReport 笔记(三) 使用JavaBean集合作为数据源 【转】
- iReport 笔记(三) 使用JavaBean集合作为数据源
- jasperreport 使用子报表(使用JavaBean集合作为数据源)
- iReport 使用JavaBean作为数据源开发报表(SSM)
- iReport 笔记(四) 使用JDBC作为数据源
- iReport 笔记(四) 使用JDBC作为数据源
- iReport 笔记(四) 使用JDBC作为数据源 【转】
- iReport —— 使用 JavaBean 作为数据源
- 图示ireport中使用javabean作数据源开发基于jasperreports报表过程
- iReport使用javabean做数据源
- ireport子报表《javabean数据源,springmvc实现》
- ireport使用javabean作为数据源的实现
- Java开源报表JasperReport、iReport4.5.1使用详解(四)-JavaBean数据源
- Java开源报表JasperReport、iReport4.5.1使用详解(四)-JavaBean数据源
- ireport 中使用javabean 作数据源开发
- ireport+jasperreport开源报表中使用javabean作为数据源的详细实现过程