Jasperreports subreport instance

Source: Internet
Author: User

The company's project needs to export data to excel and PDF. We have used VBA and poi before. This time we will use Jasper reports. During this time, we encountered a problem about subreport, which has been resolved, for your reference.

 

First of all, subreport is a concept in the ireport template editor, which is equivalent to a master-slave relationship. For example, if there is a country name in the master list, there will be different city names under each country name, this city list is subreport, which is referenced as another independent report. Here, we only show the level-2 subordination. You can use subreport to expand to a multi-layer relationship as needed.

 

This is:

 

Start to paste the Code:

Mainreport. jrxml (master table)

 

<? 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. jasperreport S. 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/> </textelemen T> <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 verticalignment = "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/> <performanceexpression> <! [CDATA [$ f {cityds}]> </performanceexpression> <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 = "allsectionsnodetai L "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 (primary table object)

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 (from Table object)

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/>

 

Testsubreport. Java (main method)

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. jrw.exporter; <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 = 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/>

 

Summary:

In fact, subreport is pre-compiled in the background together with the master table, and is set to the master table as parameter, which is referenced in the subreport label of the master table, at the same time, the datasource of subreport also transmits parameters from the field of the main table.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.