1.メインデザイン
$ cat sample01.jrxml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE jasperReport PUBLIC "//JasperReports//DTD Report Design//EN" "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"> <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="jasper_report_template" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <field name="layout" class="java.lang.String" /> <field name="subList" class="java.util.List" /> <detail> <band height="23"> <subreport> <reportElement positionType="Float" x="0" y="3" width="300" height="20" isRemoveLineWhenBlank="true" backcolor="#99ccff"/> <dataSourceExpression> new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource ($F{subList}) </dataSourceExpression> <subreportExpression class="java.lang.String"> <![CDATA[$F{layout}]]> </subreportExpression> </subreport> </band> </detail> </jasperReport>
2.サブデザイン1(サブレポート)
$ cat sample02.jrxml <?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="jasper_report_template" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <field name="pack" class="java.lang.String"/> <field name="apple" class="java.lang.String"/> <columnHeader> <band height="23"> <staticText> <reportElement x="0" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" /> </textElement> <text><![CDATA[PACK]]></text> </staticText> <staticText> <reportElement x="101" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" /> </textElement> <text><![CDATA[APPLE]]></text> </staticText> </band> </columnHeader> <detail> <band height="23"> <textField> <reportElement x="0" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="9" /> </textElement> <textFieldExpression class="java.lang.String"> <![CDATA[$F{pack}]]> </textFieldExpression> </textField> <textField> <reportElement x="101" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="9" /> </textElement> <textFieldExpression class="java.lang.String"> <![CDATA[$F{apple}]]> </textFieldExpression> </textField> </band> </detail> </jasperReport>
3.サブデザイン2(サブレポート)
$ cat sample03.jrxml <?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="jasper_report_template" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20"> <field name="pack" class="java.lang.String"/> <field name="apple" class="java.lang.String"/> <columnHeader> <band height="23"> <staticText> <reportElement x="0" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" /> </textElement> <text><![CDATA[PACK]]></text> </staticText> <staticText> <reportElement x="101" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font isBold="true" /> </textElement> <text><![CDATA[APPLE]]></text> </staticText> </band> </columnHeader> <detail> <band height="23"> <textField> <reportElement x="0" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="9" /> </textElement> <textFieldExpression class="java.lang.String"> <![CDATA[$F{pack}]]> </textFieldExpression> </textField> <textField> <reportElement x="101" y="3" width="100" height="15" /> <textElement textAlignment="Center" verticalAlignment="Middle"> <font size="9" /> </textElement> <textFieldExpression class="java.lang.String"> <![CDATA[$F{apple}]]> </textFieldExpression> </textField> </band> </detail> </jasperReport>
4.メインbean
$ cat databean.java import java.util.*; public class databean { private String layout; private Listsublist; public void setLayout(String layout) { this.layout = layout; } public String getLayout() { return(layout); } public void setSubList(List sublist) { this.sublist = sublist; } public List getSubList() { return(sublist); } }
5.サブbean
$ cat subbean.java public class subbean { private String pack; public void setPack(String pack) { this.pack = pack; } public String getPack() { return(pack); } }
6.サブbean1
$ cat subbean1.java public class subbean1 extends subbean { private String apple; public void setApple(String apple) { this.apple = apple; } public String getApple() { return(apple); } }
7.サブbean2
$ cat subbean2.java public class subbean2 extends subbean { private String orange; private String value; public void setOrange(String orange) { this.orange = orange; } public String getOrange() { return(orange); } public void setValue(String value) { this.value = value; } public String getValue() { return(value); } }
8.メインプログラム
$ cat sample01.java import java.util.*; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.data.*; public class sample01 { public static void main(String[] args) { databeanlist data = new databeanlist(); ArrayListdataList = data.getdatabeanlist(); JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList); try { Map parameters = new HashMap(); JasperFillManager.fillReportToFile("sample01.jasper", "sample01.jrprint",parameters, beanColDataSource); } catch (JRException e) { e.printStackTrace(); } System.out.println("Done filling!!! ..."); } }
9.実行
$ ls databean.java jrc.class sample01.java sample02.jrxml subbean.java subbean2.java databeanlist.java jrc.java sample01.jrxml sample03.jrxml subbean1.java $ java jrc sample01.jrxml sample01.jasper $ java jrc sample02.jrxml sample02.jasper $ java jrc sample03.jrxml sample03.jasper $ javac sample01.java $ java sample01 $ ls databean.class jrc.java sample01.jrxml subbean.class subbean2.java databean.java sample01.class sample02.jasper subbean.java databeanlist.class sample01.jasper sample02.jrxml subbean1.class databeanlist.java sample01.java sample03.jasper subbean1.java jrc.class sample01.jrprint sample03.jrxml subbean2.class
10.帳票確認
$ java net.sf.jasperreports.view.JasperViewer sample01.jrprint
0 件のコメント:
コメントを投稿