JasperReports Subreport 執行個體

來源:互聯網
上載者:User

 公司的項目需要將資料匯出為excel和pdf,之前用過vba和poi,這次要使用jasper reports,期間遇到一個關於subreport的問題,現已解決,放出給大家參考。

 

首先說明一下,subreport是iReport template編輯器中的一個概念,相當於主從關係,比如有主列表裡面是國家名稱,在每個國家名稱下,會有不同的城市名稱,這個城市列表就是subreport,是作為另一個獨立的report被引用的。這裡僅示範二級從屬關係,按需求可以再使用subreport擴充為多層關係。

 

這是:

 

開始貼代碼:

mainreport.jrxml(主表)

 

<?xml version="1.0" encoding="UTF-8"?><br /><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="mainreport" language="groovy" pageWidth="842" pageHeight="595" orientation="Landscape" columnWidth="802" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"><br /><property name="ireport.zoom" value="1.0"/><br /><property name="ireport.x" value="0"/><br /><property name="ireport.y" value="0"/><br /><parameter name="subReportObject" class="net.sf.jasperreports.engine.JasperReport" isForPrompting="false"/><br /><field name="countryName" class="java.lang.String"/><br /><field name="cityDs" class="net.sf.jasperreports.engine.data.JRBeanCollectionDataSource"/><br /><variable name="countryName_1" class="java.lang.Integer" resetType="Column" calculation="DistinctCount"><br /><variableExpression><![CDATA[$F{countryName}]]></variableExpression><br /></variable><br /><title><br /><band height="71" splitType="Stretch"><br /><frame><br /><reportElement mode="Opaque" x="85" y="0" width="546" height="24" backcolor="#66FF33"/><br /></frame><br /><staticText><br /><reportElement x="85" y="24" width="546" height="47"/><br /><textElement textAlignment="Center"><br /><font size="24"/><br /></textElement><br /><text><![CDATA[JasperReports subreport Demo (V3.7.4)]]></text><br /></staticText><br /></band><br /></title><br /><pageHeader><br /><band height="38" splitType="Stretch"><br /><staticText><br /><reportElement x="85" y="0" width="546" height="38"/><br /><textElement verticalAlignment="Top"/><br /><text><![CDATA[this demo shows how to compose a classic master-detail report. Master level is a country list, the detail level is the related cities list.]]></text><br /></staticText><br /></band><br /></pageHeader><br /><columnHeader><br /><band height="20" splitType="Stretch"><br /><staticText><br /><reportElement x="85" y="0" width="100" height="20"/><br /><textElement><br /><font isBold="true"/><br /></textElement><br /><text><![CDATA[Country Name]]></text><br /></staticText><br /></band><br /></columnHeader><br /><detail><br /><band height="94" splitType="Stretch"><br /><textField><br /><reportElement x="85" y="0" width="100" height="20"/><br /><textElement><br /><font isItalic="true"/><br /></textElement><br /><textFieldExpression class="java.lang.String"><![CDATA[$F{countryName}]]></textFieldExpression><br /></textField><br /><elementGroup><br /><subreport isUsingCache="true"><br /><reportElement positionType="Float" x="185" y="20" width="446" height="74"/><br /><dataSourceExpression><![CDATA[$F{cityDs}]]></dataSourceExpression><br /><subreportExpression class="net.sf.jasperreports.engine.JasperReport"><![CDATA[$P{subReportObject}]]></subreportExpression><br /></subreport><br /></elementGroup><br /></band><br /></detail><br /><columnFooter><br /><band height="20" splitType="Stretch"><br /><textField><br /><reportElement x="531" y="0" width="100" height="20"/><br /><textElement/><br /><textFieldExpression class="java.lang.Integer"><![CDATA[$V{countryName_1}]]></textFieldExpression><br /></textField><br /><staticText><br /><reportElement x="431" y="0" width="100" height="20"/><br /><textElement textAlignment="Justified"><br /><font isBold="true"/><br /></textElement><br /><text><![CDATA[TOTAL Country : ]]></text><br /></staticText><br /></band><br /></columnFooter><br /><pageFooter><br /><band height="20" splitType="Stretch"><br /><staticText><br /><reportElement x="350" y="0" width="100" height="20"/><br /><textElement><br /><font isItalic="true"/><br /></textElement><br /><text><![CDATA[Good luck!]]></text><br /></staticText><br /></band><br /></pageFooter><br /></jasperReport><br />

 

mainreport_subreport2.jrxml (subreport)

<?xml version="1.0" encoding="UTF-8"?><br /><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="mainreport_subreport2" language="groovy" pageWidth="802" pageHeight="555" orientation="Landscape" whenNoDataType="AllSectionsNoDetail" columnWidth="802" leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0" isIgnorePagination="true"><br /><property name="ireport.zoom" value="1.0"/><br /><property name="ireport.x" value="0"/><br /><property name="ireport.y" value="0"/><br /><field name="cityName" class="java.lang.String"/><br /><detail><br /><band height="20" splitType="Stretch"><br /><textField><br /><reportElement x="97" y="0" width="100" height="20"/><br /><textElement/><br /><textFieldExpression class="java.lang.String"><![CDATA[$F{cityName}]]></textFieldExpression><br /></textField><br /></band><br /></detail><br /></jasperReport><br />

 

Country.java (主表對象)

package com.test;</p><p>import java.util.List;</p><p>import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;</p><p>public class Country {</p><p>private int id;<br />private String countryName;<br />private JRBeanCollectionDataSource cityDs;</p><p>public Country() {<br />// TODO Auto-generated constructor stub<br />}</p><p>public Country(int id, String countryName, JRBeanCollectionDataSource cityDs) {<br />super();<br />this.id = id;<br />this.countryName = countryName;<br />this.cityDs = cityDs;<br />}</p><p>public int getId() {<br />return id;<br />}</p><p>public void setId(int id) {<br />this.id = id;<br />}</p><p>public String getCountryName() {<br />return countryName;<br />}</p><p>public void setCountryName(String countryName) {<br />this.countryName = countryName;<br />}</p><p>public JRBeanCollectionDataSource getCityDs() {<br />return cityDs;<br />}</p><p>public void setCityDs(JRBeanCollectionDataSource cityDs) {<br />this.cityDs = cityDs;<br />}</p><p>}<br />

 

City.java(從表對象)

package com.test;</p><p>public class City {</p><p>private int id;<br />private String cityName;</p><p>public City() {<br />// TODO Auto-generated constructor stub<br />}</p><p>public City(int id, String cityName) {<br />super();<br />this.id = id;<br />this.cityName = cityName;<br />}</p><p>public int getId() {<br />return id;<br />}</p><p>public void setId(int id) {<br />this.id = id;<br />}</p><p>public String getCityName() {<br />return cityName;<br />}</p><p>public void setCityName(String cityName) {<br />this.cityName = cityName;<br />}<br />}<br />

 

TestSubReport.java (main方法)

package com.test;</p><p>/*<br /> * jasper report v3.7.4<br /> */</p><p>import java.io.File;<br />import java.util.ArrayList;<br />import java.util.Calendar;<br />import java.util.HashMap;<br />import java.util.List;<br />import java.util.Map;</p><p>import net.sf.jasperreports.engine.JRException;<br />import net.sf.jasperreports.engine.JRExporter;<br />import net.sf.jasperreports.engine.JRExporterParameter;<br />import net.sf.jasperreports.engine.JasperCompileManager;<br />import net.sf.jasperreports.engine.JasperFillManager;<br />import net.sf.jasperreports.engine.JasperPrint;<br />import net.sf.jasperreports.engine.JasperReport;<br />import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;<br />import net.sf.jasperreports.engine.design.JasperDesign;<br />import net.sf.jasperreports.engine.export.JRPdfExporter;<br />import net.sf.jasperreports.engine.export.JRXlsExporter;<br />import net.sf.jasperreports.engine.xml.JRXmlLoader;</p><p>public class TestSubReport {</p><p>public TestSubReport() {<br />// TODO Auto-generated constructor stub<br />}</p><p>@SuppressWarnings("unchecked")<br />public static void main(String[] args) throws JRException {<br />System.out.println("start");<br />//get *.jrxml<br />JasperDesign jDesign = JRXmlLoader.load(new File("/test2/mainreport.jrxml"));<br />JasperDesign jDesign2 = JRXmlLoader.load(new File("/test2/mainreport_subreport2.jrxml"));<br />//export filename<br />String fileName = "subReport-on"+Calendar.getInstance().getTimeInMillis(); </p><p>//get *.jasper<br />JasperReport jReport = JasperCompileManager.compileReport(jDesign);<br />JasperReport jReport2 = JasperCompileManager.compileReport(jDesign2);</p><p>//fill data<br />List data = new ArrayList();</p><p>List list1 = new ArrayList();<br />list1.add(new City(1,"suzhou"));<br />list1.add(new City(2,"shanghai"));<br />list1.add(new City(3,"beijing"));</p><p>List list2 = new ArrayList();<br />list2.add(new City(4,"New York"));<br />list2.add(new City(5,"San Francisco"));</p><p>List list3 = new ArrayList();<br />list3.add(new City(6, "London"));<br />list3.add(new City(7, "Nottinghamshire"));</p><p>JRBeanCollectionDataSource ds1 = new JRBeanCollectionDataSource(list1);<br />JRBeanCollectionDataSource ds2 = new JRBeanCollectionDataSource(list2);<br />JRBeanCollectionDataSource ds3 = new JRBeanCollectionDataSource(list3);</p><p>data.add(new Country(1 , "China" , ds1));<br />data.add(new Country(2 , "USA" , ds2));<br />data.add(new Country(3 , "UK" , ds3));</p><p>Map map = new HashMap();<br />map.put("subReportObject", jReport2);//subReport put here.</p><p>JasperPrint jPrint = JasperFillManager.fillReport(jReport, map, new JRBeanCollectionDataSource(data));</p><p>//export with pdf<br />JRExporter jExporter = new JRPdfExporter();<br />jExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName+".pdf");<br />jExporter.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);<br />jExporter.exportReport();</p><p>//export with xls<br />JRXlsExporter jXlsExporter = new JRXlsExporter();<br />jXlsExporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, fileName+".xls");<br />jXlsExporter.setParameter(JRExporterParameter.JASPER_PRINT, jPrint);<br />jXlsExporter.exportReport();</p><p>System.out.println(fileName);<br />System.out.println("end");<br />}<br />}<br />

 

總結:

其實subreport是和主表一起在後台被先行編譯,然後作為parameter 設定給主表,在主表的subreport標籤中被引用,同時subreport的datasource同樣是從主表的field中傳參過去。 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.