[英]How to show JRBeanCollectionDataSource data with help of Table component?
I need to show JRBeanCollectionDataSource data in Table component (JasperReports).我需要在表组件 (JasperReports) 中显示 JRBeanCollectionDataSource 数据。
Here is my template, ShowPerson.jrxml file:这是我的模板 ShowPerson.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="ShowPerson" pageWidth="612" pageHeight="792" whenNoDataType="NoDataSection" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="304c4c4e-c99a-4399-8081-748d3b7c0b8c">
<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" whenResourceMissingType="Empty" uuid="63b01547-bce2-47c9-ba15-666f94d11387">
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="name" class="java.lang.String"/>
<field name="age" class="java.lang.Integer"/>
</subDataset>
<parameter name="INFO" class="java.lang.String"/>
<title>
<band height="40" splitType="Stretch">
<staticText>
<reportElement uuid="e96450a8-8358-4cae-a094-3add06d57de2" x="0" y="20" width="56" height="20"/>
<textElement/>
<text><![CDATA[Info]]></text>
</staticText>
<textField>
<reportElement uuid="e21e9932-ebfe-4bb5-904d-ea99e141866b" x="56" y="20" width="100" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$P{INFO}]]></textFieldExpression>
</textField>
</band>
</title>
<detail>
<band height="40" splitType="Stretch">
<componentElement>
<reportElement uuid="dea5d821-81b6-434b-ae27-4f3a0268e805" key="table 1" x="0" y="0" width="572" height="40"/>
<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="39f8f0bf-8a2b-44f4-9a4c-0c873da79fba">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[]]></datasetParameterExpression>
</datasetParameter>
<dataSourceExpression><![CDATA[$P{REPORT_DATA_SOURCE}]]></dataSourceExpression>
</datasetRun>
<jr:column width="169" uuid="aae649c4-6a69-485f-8a0d-87022df793ee">
<jr:tableHeader height="20" rowSpan="1">
<staticText>
<reportElement uuid="795dac43-0ff0-482c-89a0-7dac3b27d513" x="0" y="0" width="169" height="20"/>
<textElement/>
<text><![CDATA[Name]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement uuid="4f1ab13a-d776-4cd5-a2a7-a5cf23360816" x="0" y="0" width="169" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column width="173" uuid="a6997eea-131e-4555-80e6-a20d4decb18f">
<jr:tableHeader height="20" rowSpan="1">
<staticText>
<reportElement uuid="5f6e2413-8025-47ca-9638-9609ea13f93f" x="0" y="0" width="173" height="20"/>
<textElement/>
<text><![CDATA[Age]]></text>
</staticText>
</jr:tableHeader>
<jr:detailCell height="20" rowSpan="1">
<textField>
<reportElement uuid="195d51a0-9e45-4201-ad67-d3026ce2e72c" x="0" y="0" width="173" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[$F{age}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
</jasperReport>
My POJO:我的POJO:
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Main class for building report:建筑报告的主要类:
public class OpenReport {
public static void main(String[] args) throws JRException {
Map<String, Object> peopleMap = new HashMap<>();
peopleMap.put("Sisco", 17);
peopleMap.put("Eve", 19);
peopleMap.put("John", 20);
peopleMap.put("George", 21);
peopleMap.put("Steve", 18);
ArrayList<Person> dataList = new ArrayList<Person>();
for(Map.Entry<String, Object> personMap : peopleMap.entrySet()) {
Person person = new Person();
person.setName(personMap.getKey());
person.setAge(Integer.valueOf(personMap.getValue().toString()));
dataList.add(person);
}
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, beanColDataSource);
JFrame frame = new JFrame("Report");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(new JRViewer(jasperPrint));
frame.pack();
frame.setVisible(true);
}
}
1.Send your datasource from the server as a parameter, and fill the report with a different one (can be empty). 1.从服务器发送您的数据源作为参数,并用不同的(可以为空)填充报告。
JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", beanColDataSource);
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
2.Decalre a new parameter in the report of type JRBeanCollectionDataSource 2.在JRBeanCollectionDataSource类型的报表中Decalre一个新参数
<parameter name="DS1" class="net.sf.jasperreports.engine.JRBeanCollectionDataSource"/>
3.Set TableDatasource to use the $P{DS1} parameter. 3. 设置 TableDatasource 以使用 $P{DS1} 参数。
<jr:table ...>
<datasetRun subDataset="Table Dataset 1">
<datasetParameter name="REPORT_DATA_SOURCE">
<datasetParameterExpression><![CDATA[$P{DS1}]]></datasetParameterExpression>
</datasetParameter>
</datasetRun>
.....
4.Declare the fields (name, age) in Table Dataset 1. 4.声明表数据集1中的字段(姓名、年龄)。
5.In the table, in the DetailBand add a TextField in each column with the corresponding field ($F{name} and $F{age} respectively). 5.在表中的DetailBand 中,在每列中添加一个TextField 和相应的字段(分别为$F{name} 和$F{age})。
@laura: I get new error when trying set the TableDatasource to use the $P{DS1} parameter. @laura:尝试将 TableDatasource 设置为使用 $P{DS1} 参数时出现新错误。 net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
net.sf.jasperreports.engine.JRBeanCollectionDataSource 无法解析为类型
Now, I have found the solution, maybe not the best.现在,我找到了解决方案,也许不是最好的。
Map parameters = new HashMap();
parameters.put("INFO", "Hello");
parameters.put("DS1", dataList);
JasperReport report = (JasperReport) JRLoader.loadObject("src/test/ireport/ShowPerson.jasper");
JasperPrint jasperPrint = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
Modify the class of $P{DS1} to java.util.Collection or java.util.List将 $P{DS1} 的类修改为 java.util.Collection 或 java.util.List
And set the table datasource to并将表数据源设置为
new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{DS1})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.