[英]How to generate a PDF subreport in a Java web application using lists?
I'm generating reports with lists and parameters, no database, no query. 我正在使用列表和参数生成报告,没有数据库,没有查询。 I'm working with JasperSoft Studio 6 (jasperreports-6.5.1.jar, iText-2.1.7.js2.jar), Eclipse, JSP, Java 7 and Tomcat 7. 我正在使用JasperSoft Studio 6(jasperreports-6.5.1.jar,iText-2.1.7.js2.jar),Eclipse,JSP,Java 7和Tomcat 7。
I can generate a simple list like this: 我可以生成一个这样的简单列表:
Sample.java Sample.java
public class Sample {
private String value;
public Sample(String value) {this.value = value;}
public String getValue() {return value;}
}
SampleServlet.java SampleServlet.java
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
InputStream jrxml = getServletContext().getResourceAsStream("WEB-INF/pdf/Sample.jrxml");
JasperReport report = JasperCompileManager.compileReport(jrxml);
List<Sample> list = Arrays.asList(new Sample("One"), new Sample("Two"), new Sample("Three"));
JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(list);
JasperPrint print = JasperFillManager.fillReport(report, null, source);
response.setContentType("application/pdf");
ServletOutputStream output = response.getOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(output));
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
}
}
Sample.jrxml Sample.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1 -->
<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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="ec148bc4-3203-4f96-94ac-2c16820bcfb3">
<queryString>
<![CDATA[]]>
</queryString>
<field name="value" class="java.lang.String"/>
<detail>
<band height="32" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="555" height="32" uuid="c0ccbbff-6099-4565-91f2-53e78f7b97be"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</band>
</detail>
</jasperReport>
This outputs a PDF with a list (each line could be a separate page): 这将输出带有列表的PDF(每行可以是单独的页面):
One
Two
Three
But I need to add sublists (subreports) in there... 但我需要在那里添加子列表(子报告)......
List<Sample> list = Arrays.asList(new Sample("One:"), new Sample("Two:"), new Sample("Three:"));
List<List<SubSample>> sublists = Arrays.asList(
Arrays.asList(new SubSample("First"), new SubSample("Single"), new SubSample("Solo")),
Arrays.asList(new SubSample("Second"), new SubSample("Double"), new SubSample("Duo")),
Arrays.asList(new SubSample("Third"), new SubSample("Triple"), new SubSample("Trio"))
);
...that should output a PDF like this (each group could be a separate page): ...应输出这样的PDF(每个组可以是一个单独的页面):
One:
First
Single
Solo
Two:
Second
Double
Duo
Three:
Third
Triple
Trio
How can I achieve this? 我怎样才能做到这一点?
I got it. 我知道了。 Everything's done in the main report, you do nothing at all to the subreport. 在主报告中完成所有操作后,您根本不对子报表执行任何操作。
Being so, the changes are: 因此,变化是:
List<SubSample>
sublist ; 在Sample.java中,新的字段List<SubSample>
子列表 ; java.util.List
sublist and a new Parameter java.lang.String
subreport ; 在Sample.jrxml中,一个新的Field java.util.List
子列表和一个新的Parameter java.lang.String
子报告 ; Expression
as $P{subreport} and Data Source Expression
as new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{sublist}) . 第二个Detail带,带有一个新的Subreport元素,其Expression
为$ P {subreport} , Data Source Expression
为new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($ F {sublist}) 。 The result: 结果:
SubSample.java SubSample.java
Copy of the original Sample.java
SubSample.jrxml SubSample.jrxml
Copy of the original Sample.jrxml (with a different uuid)
Sample.java Sample.java
public class Sample {
private String value;
private List<SubSample> sublist;
public Sample(String value) {this.value = value;}
public String getValue() {return value;}
public List<SubSample> getSublist() {return sublist;}
public void setSublist(List<SubSample> sublist) {this.sublist = sublist;}
}
Sample.jrxml Sample.jrxml
<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.5.1.final using JasperReports Library version 6.5.1 -->
<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="Blank_A4" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="17771092-9052-49a8-8301-28b1bdf97288">
<parameter name="subreport" class="java.lang.String"/>
<queryString>
<![CDATA[]]>
</queryString>
<field name="value" class="java.lang.String"/>
<field name="sublist" class="java.util.List"/>
<detail>
<band height="32" splitType="Stretch">
<textField>
<reportElement x="0" y="0" width="555" height="32" uuid="c0ccbbff-6099-4565-91f2-53e78f7b97be"/>
<textFieldExpression><![CDATA[$F{value}]]></textFieldExpression>
</textField>
</band>
<band height="32">
<subreport>
<reportElement x="0" y="0" width="555" height="32" uuid="e51bd9df-f1c5-4776-9438-8854461bad2a"/>
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{sublist})]]></dataSourceExpression>
<subreportExpression><![CDATA[$P{subreport}]]></subreportExpression>
</subreport>
</band>
</detail>
</jasperReport>
SampleServlet.java SampleServlet.java
public class SampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
try {
InputStream jrxml = getServletContext().getResourceAsStream("WEB-INF/pdf/Sample.jrxml");
JasperReport report = JasperCompileManager.compileReport(jrxml);
String subjrxml = getServletContext().getRealPath("WEB-INF/pdf/SubSample.jrxml");
String subreport = JasperCompileManager.compileReportToFile(subjrxml);
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("subreport", subreport);
List<Sample> list = Arrays.asList(new Sample("One:"), new Sample("Two:"), new Sample("Three:"));
List<List<SubSample>> sublists = Arrays.asList(
Arrays.asList(new SubSample("First"), new SubSample("Single"), new SubSample("Solo")),
Arrays.asList(new SubSample("Second"), new SubSample("Double"), new SubSample("Duo")),
Arrays.asList(new SubSample("Third"), new SubSample("Triple"), new SubSample("Trio"))
);
for (int i = 0; i < list.size(); i++)
list.get(i).setSublist(sublists.get(i));
JRBeanCollectionDataSource source = new JRBeanCollectionDataSource(list);
JasperPrint print = JasperFillManager.fillReport(report, parameters, source);
response.setContentType("application/pdf");
ServletOutputStream output = response.getOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(print));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(output));
exporter.exportReport();
} catch (JRException e) {
e.printStackTrace();
}
}
}
PDF output: PDF输出:
One:
First
Single
Solo
Two:
Second
Double
Duo
Three:
Third
Triple
Trio
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.