iReport+JasperPrint 使用javaBean 集合和子集合创建报表

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hwd15219166920/article/details/79911483

先上效果图    使用的是javaBean List    javaBean 中还有一个List 属性

第一步  创建JAVA bean

public class Jdetail {
	
	
	public String dpName;
	public String dpCode;
	public String getDpName() {
		return dpName;
	}
	public void setDpName(String dpName) {
		this.dpName = dpName;
	}
	public String getDpCode() {
		return dpCode;
	}
	public void setDpCode(String dpCode) {
		this.dpCode = dpCode;
	}
	
	

}

public class Jtest {

	private String	code;
    private String	name;
    private List<Jdetail> detls;
      
    public  Jtest(String code,String name){
    	  this.code=code;
    	  this.name=name;
    }
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	public List<Jdetail> getDetls() {
		return detls;
	}
	public void setDetls(List<Jdetail> detls) {
		this.detls = detls;
	}

	
}

第二部 创建模板

打开ireport (这里使用的是5.1版本)


把Title 和其他的宽度全部设置成0  只留下Detail

左边右键列表选择 Edit_Query


将JavaBean 添加到模板中  (这里首先要将编译好的带有Jtest.class 的jar 添加进来  才能识别。 如果不知道怎么加 看最下面)


将Field 拖进来


再拖一个table控件进来


一直下一步


编辑列  (留下表头和 表体就够了)


右键Table 选择Edit tableSources


修改表格数据源类型  将List 传递给集合  

new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{detls})


选中表格数据源  右键Edit Query 添加JavaBean    



选择模板右键 编译


OK 啦  下面是 JAVA 运行代码

 public static void main(String[] args) {
	    	
	    	List<Jtest> data=new ArrayList<Jtest>();
	    	Jtest test1=	new Jtest("111", "SSS");
	    	test1.setDetls(new ArrayList<Jdetail>());
	    	Jdetail dto1=new Jdetail();
	    	dto1.setDpCode("2222");
	    	dto1.setDpName("001244");
	    	test1.getDetls().add(dto1);
	    	Jdetail dto2=new Jdetail();
	    	dto2.setDpCode("3333");
	    	dto2.setDpName("001333");
	    	test1.getDetls().add(dto2);
	    	data.add(test1);
	    	
	    	Jtest test2=	new Jtest("222", "ZZZ");
	    	test2.setDetls(new ArrayList<Jdetail>());
	    	Jdetail dto3=new Jdetail();
	    	dto3.setDpCode("4444");
	    	dto3.setDpName("00666");
	    	test2.getDetls().add(dto3);
	    	data.add(test2);
	    	   try {
		        	
		            HashMap<String, Object> paramsMap = new HashMap<String, Object>();
		            // 第二种配置数据源的方式 InputStream
		            paramsMap.put("dtls", data);
		            // 注意更改路径
		        	 File exe_rpt = new File(("E:/IreportData/report2.jasper"));  
		        	 
		        	 JRDataSource dataSource = new JRBeanCollectionDataSource(data);
		        	 
		        	JasperPrint jasperPrint = JasperFillManager.fillReport(exe_rpt.getPath(), paramsMap,dataSource);

		           
		            // 加载模板
		          //  JasperReport report = JasperCompileManager.compileReport(design);
		            // 填充数据
		         //   JasperPrint print = JasperFillManager.fillReport(jasperPrint, paramsMap);
		            // 预览显示
		            JasperViewer.viewReport(jasperPrint);

		        } catch (Exception e) {
		            // TODO Auto-generated catch block
		            e.printStackTrace();
		        }
	    	
		}


最后上传ireport 模板源码


<?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="report3" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="9914c716-8e00-46b0-a917-af7884b1ead6">
	<property name="ireport.zoom" value="1.0"/>
	<property name="ireport.x" value="0"/>
	<property name="ireport.y" value="0"/>
	<style name="table">
		<box>
			<pen lineWidth="1.0" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
		<box>
			<pen lineWidth="0.5" lineColor="#000000"/>
		</box>
	</style>
	<subDataset name="Table Dataset 1" uuid="3254b40d-f951-41ec-8cbb-8fcfcd7df3a3">
		<queryString>
			<![CDATA[]]>
		</queryString>
		<field name="dpCode" class="java.lang.String">
			<fieldDescription><![CDATA[dpCode]]></fieldDescription>
		</field>
		<field name="dpName" class="java.lang.String">
			<fieldDescription><![CDATA[dpName]]></fieldDescription>
		</field>
	</subDataset>
	<queryString>
		<![CDATA[]]>
	</queryString>
	<field name="code" class="java.lang.String">
		<fieldDescription><![CDATA[code]]></fieldDescription>
	</field>
	<field name="detls" class="java.util.List">
		<fieldDescription><![CDATA[detls]]></fieldDescription>
	</field>
	<field name="name" class="java.lang.String">
		<fieldDescription><![CDATA[name]]></fieldDescription>
	</field>
	<background>
		<band splitType="Stretch"/>
	</background>
	<title>
		<band splitType="Stretch"/>
	</title>
	<pageHeader>
		<band splitType="Stretch"/>
	</pageHeader>
	<columnHeader>
		<band splitType="Stretch"/>
	</columnHeader>
	<detail>
		<band height="178" splitType="Stretch">
			<staticText>
				<reportElement x="32" y="18" width="100" height="20" uuid="fedbc6d5-9f9c-4621-9b34-77c080b372ac"/>
				<text><![CDATA[编号]]></text>
			</staticText>
			<textField>
				<reportElement x="132" y="18" width="100" height="20" uuid="68ae273b-03fc-4dfc-ae09-e06f8dafabba"/>
				<textFieldExpression><![CDATA[$F{code}]]></textFieldExpression>
			</textField>
			<staticText>
				<reportElement x="286" y="18" width="100" height="20" uuid="4c1a2059-a41d-4e7f-bbbc-92b5f4977596"/>
				<text><![CDATA[名称]]></text>
			</staticText>
			<textField>
				<reportElement x="410" y="18" width="100" height="20" uuid="d5ec6426-79f5-4df2-a75c-34c1e914a54e"/>
				<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
			</textField>
			<componentElement>
				<reportElement key="table" style="table" x="49" y="58" width="381" height="87" uuid="443f3733-c6b3-48ff-8772-1c828cd8497f"/>
				<jr:table xmlns:jr="http://jasperreports.sourceforge.net/jasperreports/components" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports/components http://jasperreports.sourceforge.net/xsd/components.xsd">
					<datasetRun subDataset="Table Dataset 1" uuid="2a122f78-7803-4568-ba47-5631b7a260e5">
						<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{detls})]]></dataSourceExpression>
					</datasetRun>
					<jr:column width="90" uuid="074810b7-8380-4bbf-a03a-847ae5456678">
						<jr:columnHeader height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="90" height="30" uuid="c914fa6f-4a79-4027-a485-155d76d1ca74"/>
								<text><![CDATA[子名称]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell height="30" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="90" height="30" uuid="719c707a-3ecb-4be6-b29c-65ca1e39c8a8"/>
								<textFieldExpression><![CDATA[$F{dpCode}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
					<jr:column width="90" uuid="c39fdfa0-4e4f-4be0-8d4a-8cfbfd11e9ee">
						<jr:columnHeader height="30" rowSpan="1">
							<staticText>
								<reportElement x="0" y="0" width="90" height="30" uuid="a1ed950f-ede0-45ab-ab8c-c9d78e4f3471"/>
								<text><![CDATA[子编号]]></text>
							</staticText>
						</jr:columnHeader>
						<jr:detailCell style="table_TD" height="30" rowSpan="1">
							<textField>
								<reportElement x="0" y="0" width="90" height="30" uuid="981a457c-2ad2-40da-9bd9-0203824b8b94"/>
								<textFieldExpression><![CDATA[$F{dpName}]]></textFieldExpression>
							</textField>
						</jr:detailCell>
					</jr:column>
				</jr:table>
			</componentElement>
		</band>
	</detail>
	<columnFooter>
		<band splitType="Stretch"/>
	</columnFooter>
	<pageFooter>
		<band splitType="Stretch"/>
	</pageFooter>
	<summary>
		<band splitType="Stretch"/>
	</summary>
</jasperReport>

展开阅读全文

没有更多推荐了,返回首页