我在JasperReports中使用ArrayList作为我的主要数据源时遇到问题。
我的程序是一个简单的 Java 程序,我有一个非常简单的报告。我的ArrayList中有两个数据对象,我用JRBeanCollectionDataSource将它们包装起来,然后通过JasperFillManager将其传递给JRPrint。我的报告生成了,但只生成了第一个数组项。
我基本上是在遵循本教程,然后将另一个项目添加到ArrayList。但只有一个打印出来。
代码片段:
ArrayList<ProtoReport> listOfReports = new ArrayList<ProtoReport>();
ProtoReport protoReport1 = new ProtoReport();
ProtoReport protoReport2 = new ProtoReport();
//Simple Fields and text
protoReport1.setTitle("Example Fact Sheet");
protoReport2.setTitle("2nd Fact Sheet");
//add all reports to the list
listOfReports.add(protoReport1);
listOfReports.add(protoReport2);
//and wrap the ArrayList in a JRBeanCollectionDataSource
JRBeanCollectionDataSource beanBurritoWrap = new JRBeanCollectionDataSource(listOfReports);
//build the jasper report
JasperReport jasperReport;
JasperPrint jasperPrint;
HashMap<String, Object> hashMap = new HashMap<>();
boolean reportCreated;
try {
jasperReport = JasperCompileManager.compileReport(jrxmlLocation);
jasperPrint = JasperFillManager.fillReport(jasperReport, hashMap, beanBurritoWrap);
JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
reportCreated = true;
} catch (JRException e) {
e.printStackTrace();
reportCreated = false;
}
//Report on build status
System.out.println("Jasper Report built: " + reportCreated);
ProtoReport 类基本上只是报表字段、表格和图表数据的容器
例如:
package org.reportprotojava.protosheet;
import java.util.ArrayList;
public class ProtoReport {
private String outputFileName;
private String title;
private String logoLocation;
private String paragraphText;
private ArrayList<String> tableData;
private String picLocation;
private int[][] graphData; //TODO decide how to store chart data
private ChartData chartData;
private String path;
//default constructor
public ProtoReport() {
// Initialize object fields
outputFileName = "PrototypeReport";
title = "Prototype Report";
paragraphText = "Default text";
tableData = new ArrayList<String>();
chartData = new ChartData();
//set path to working directory
path = System.getProperty("user.dir");
//default to assumed report location
//(ie same folder as .jrxml and .jasper files)
logoLocation = path + "\\reports";
picLocation = path + "\\reports";
}
加上一个参数化的构造函数,然后是 getter 和 setter 等
编译没有问题,第一个报告导出为pdf没有问题。我得到的唯一警告是:
log4j:WARN No appenders could be found for logger (net.sf.jasperreports.engine.xml.JRXmlDigesterFactory).
log4j:WARN Please initialize the log4j system properly.
但我不认为这与这个问题有任何关系(如果这不是真的,请纠正我)。
这是我的 .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="ReportPrototype.jrxml" pageWidth="595" pageHeight="842" columnWidth="495" leftMargin="57" rightMargin="43" topMargin="43" bottomMargin="43" uuid="10825c57-f953-4166-bf03-8ecabe8a8f47">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="128"/>
<subDataset name="ChartData" uuid="ebdb66fc-82b2-489c-8187-731eed51dd7a">
<field name="xAxis" class="java.util.List"/>
<field name="yAxis" class="java.util.List"/>
</subDataset>
<queryString language="SQL">
<![CDATA[]]>
</queryString>
<field name="title" class="java.lang.String"/>
<field name="logoLocation" class="java.lang.String"/>
<field name="picLocation" class="java.lang.String"/>
<field name="chartData" class="java.lang.Object"/>
<field name="xAxis" class="java.util.List"/>
<field name="yAxis" class="java.util.List"/>
<title>
<band height="115" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="">
<reportElement uuid="519c6bb5-72f9-4c25-8e91-47865ae0c9df" mode="Opaque" x="0" y="70" width="495" height="45" forecolor="#000099"/>
<textElement>
<font size="26"/>
</textElement>
<textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
</textField>
<image>
<reportElement uuid="f989f871-32ea-4f13-ae3f-3f487cde76dd" x="295" y="0" width="200" height="42"/>
<imageExpression><![CDATA[$F{logoLocation}]]></imageExpression>
</image>
</band>
</title>
<pageHeader>
<band height="45" splitType="Stretch">
<staticText>
<reportElement uuid="34054fe4-f5c8-4154-b11e-558ad49c9bed" x="195" y="14" width="100" height="20"/>
<textElement/>
<text><![CDATA[Static text]]></text>
</staticText>
</band>
</pageHeader>
<columnHeader>
<band height="105" splitType="Stretch">
<image>
<reportElement uuid="3759a707-32a4-49ef-a9c6-b0ad7136f738" x="242" y="0" width="253" height="105"/>
<imageExpression><![CDATA[$F{picLocation}]]></imageExpression>
</image>
</band>
</columnHeader>
<detail>
<band height="184" splitType="Stretch">
<xyLineChart>
<chart>
<reportElement uuid="3b739a73-6612-42b0-bdf9-46f5d9a9899d" x="0" y="0" width="495" height="184"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<dataset>
<datasetRun subDataset="ChartData" uuid="d84314f7-4580-4b2b-a190-b0bbe4ea63df">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{chartData})]]></dataSourceExpression>
</datasetRun>
</dataset>
</xyDataset>
<linePlot>
<plot/>
</linePlot>
</xyLineChart>
</band>
</detail>
<columnFooter>
<band height="45" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="45" splitType="Stretch"/>
</pageFooter>
<lastPageFooter>
<band height="45" splitType="Stretch"/>
</lastPageFooter>
<summary>
<band height="45" splitType="Stretch"/>
</summary>
有谁知道我做错了什么?
编辑
好的,我根据 AlexK 的建议进行了一些更改,将我的所有字段、图表和表格移动到 Detail 1 带中,但是现在当我去编译时,我收到以下错误:
net.sf.jasperreports.engine.JRException: Byte data not found at : C:\Users\eljaydub\workspace\ReportProtoJava\reports
at net.sf.jasperreports.repo.RepositoryUtil.getBytesFromLocation(RepositoryUtil.java:324)
at net.sf.jasperreports.engine.RenderableUtil.getRenderable(RenderableUtil.java:121)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluateImage(JRFillImage.java:505)
at net.sf.jasperreports.engine.fill.JRFillImage.evaluate(JRFillImage.java:442)
at net.sf.jasperreports.engine.fill.JRFillElementContainer.evaluate(JRFillElementContainer.java:257)
at net.sf.jasperreports.engine.fill.JRFillBand.evaluate(JRFillBand.java:457)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillColumnBand(JRVerticalFiller.java:2037)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillDetail(JRVerticalFiller.java:771)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReportContent(JRVerticalFiller.java:301)
at net.sf.jasperreports.engine.fill.JRVerticalFiller.fillReport(JRVerticalFiller.java:148)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:909)
at net.sf.jasperreports.engine.fill.JRBaseFiller.fill(JRBaseFiller.java:841)
at net.sf.jasperreports.engine.fill.JRFiller.fill(JRFiller.java:88)
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:653)
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:969)
at org.reportprotojava.protosheet.Program.main(Program.java:130)
我做了一些谷歌搜索,发现大多数时候这是由与图像文件位置相关的问题引起的,所以我用这个修改了我的代码:
//Locations
//Absolute
protoReport1.setLogoLocation("C:\\Users\\eljaydub\\workspace\\ReportProtoJava\\reports\\logo.gif");
protoReport1.setPicLocation("C:\\Users\\eljaydub\\workspace\\ReportProtoJava\\reports\\portfolio.jpg");
//Relative
// protoReport1.setLogoLocation(protoReport1.getPath() + "\\reports\\logo.gif");
// protoReport1.setPicLocation(protoReport1.getPath() + "\\reports\\portfolio.jpg");
并尝试了相对路径和绝对路径,但得到相同的错误。这些是相关的还是我应该为此错误创建一个新帖子?
编辑
解决了!AlexK 的建议确实解决了第一个问题,然后揭示了第二个问题:当报告转到下一个数组元素protoReport2时,它发现该对象的图像链接是我的默认构造函数提供的那些,这只是猜测可能图像文件的名称和位置。然后抛出异常,因为两个图像的图像属性onErrorType都设置为错误。我只需将 iReports 中的属性更改为Icon并且生成的报告没有进一步的问题,我得到了我的预期:两个数组元素打印在不同的页面上,第二个使用默认构造函数值。谢谢您的帮助!