我现在花了 12 个小时的大部分时间试图弄清楚如何让图表和表格在 jasper 中工作。这篇文章是关于表格的(对于我的图表问题,可能是相同的问题,请参见此处)
我想使用 iReports 调色板中的表格工具制作一个包含两列的表格。我在ProtoReport数据对象列表中有一个String的List(以前是一个ArrayList,但我现在正在尝试一个 List,因为我看过的教程之一使用了它,而且他们没有问题) 。ProtoReport列表是我包装在JRBeanCollectionDataSource中并传递给 JasperFill 的列表。我希望tableData 列表填充我的表的列之一。当我根据我对 Jasper 的有限经验让我认为我应该设置它时,我收到以下错误:
net.sf.jasperreports.engine.JRException: Errors were encountered when compiling report expressions class file:
1. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getValue())); //$JR_EXPR_ID=12$
<---------------------------------------------------->
2. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getOldValue())); //$JR_EXPR_ID=12$
<---------------------------------------------------->
3. net.sf.jasperreports.engine.JRBeanCollectionDataSource cannot be resolved to a type
value = new net.sf.jasperreports.engine.JRBeanCollectionDataSource(((java.util.List)field_tableData.getValue())); //$JR_EXPR_ID=12$
<---------------------------------------------------->
3 errors
at net.sf.jasperreports.engine.design.JRAbstractCompiler.compileReport(JRAbstractCompiler.java:204)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:240)
at net.sf.jasperreports.engine.JasperCompileManager.compile(JasperCompileManager.java:173)
at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:448)
at org.reportprotojava.protosheet.Program.main(Program.java:134)
这是我的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="0.75"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="128"/>
<style name="table">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<topPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<topPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<topPen lineWidth="0.5" lineColor="#000000"/>
<bottomPen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 1">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table 1_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 1_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 1_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#EFF7FF"/>
</conditionalStyle>
</style>
<style name="table 2">
<box>
<pen lineWidth="1.0" lineColor="#000000"/>
</box>
</style>
<style name="table 2_TH" mode="Opaque" backcolor="#F0F8FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 2_CH" mode="Opaque" backcolor="#BFE1FF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
</style>
<style name="table 2_TD" mode="Opaque" backcolor="#FFFFFF">
<box>
<pen lineWidth="0.5" lineColor="#000000"/>
</box>
<conditionalStyle>
<conditionExpression><![CDATA[new Boolean($V{REPORT_COUNT}.intValue()%2==0)]]></conditionExpression>
<style backcolor="#EFF7FF"/>
</conditionalStyle>
</style>
<subDataset name="ChartData" uuid="fc9ec0af-3e1a-40a7-8eb4-9ad30a266dee"/>
<subDataset name="Table Dataset 1" uuid="70531f85-19bf-4bd7-b801-6ed08b189b34">
<field name="tableData" 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="paragraphText" class="java.lang.String"/>
<field name="tableData" class="java.util.List"/>
<detail>
<band height="756" splitType="Stretch">
<textField isStretchWithOverflow="true" pattern="">
<reportElement uuid="519c6bb5-72f9-4c25-8e91-47865ae0c9df" mode="Opaque" x="38" y="42" width="378" height="45" forecolor="#000099"/>
<textElement textAlignment="Center" verticalAlignment="Middle" markup="html">
<font size="26"/>
</textElement>
<textFieldExpression><![CDATA[$F{title}]]></textFieldExpression>
</textField>
<image onErrorType="Icon">
<reportElement uuid="3759a707-32a4-49ef-a9c6-b0ad7136f738" x="216" y="273" width="279" height="246"/>
<imageExpression><![CDATA[$F{picLocation}]]></imageExpression>
</image>
<image onErrorType="Icon">
<reportElement uuid="f989f871-32ea-4f13-ae3f-3f487cde76dd" x="295" y="0" width="200" height="42"/>
<imageExpression><![CDATA[$F{logoLocation}]]></imageExpression>
</image>
<xyLineChart>
<chart>
<reportElement uuid="ae87fc13-b92e-4a2a-b218-d395343f6028" x="0" y="537" width="495" height="203"/>
<chartTitle/>
<chartSubtitle/>
<chartLegend/>
</chart>
<xyDataset>
<dataset>
<datasetRun subDataset="ChartData" uuid="de7fb84d-17ea-4e5e-82bf-2015e72e4982"/>
</dataset>
</xyDataset>
<linePlot>
<plot/>
</linePlot>
</xyLineChart>
<textField>
<reportElement uuid="565b981c-ca6f-4eab-ab3e-683b1e2b4d03" stretchType="RelativeToTallestObject" mode="Opaque" x="0" y="103" width="495" height="144" backcolor="#CCCCCC"/>
<textElement markup="html">
<font size="6"/>
</textElement>
<textFieldExpression><![CDATA[$F{paragraphText}]]></textFieldExpression>
</textField>
<componentElement>
<reportElement uuid="8eb2d942-e4be-4b86-b409-9e1b91f6b4c4" key="table 2" style="table 2" x="13" y="273" width="180" height="246"/>
<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="40906b08-698d-4979-ae31-7f1689859954">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JRBeanCollectionDataSource($F{tableData})]]></dataSourceExpression>
</datasetRun>
<jr:column uuid="f1282add-60a7-49d7-b66d-c8bd912dc161" width="90">
<jr:tableHeader style="table 2_TH" height="30"/>
<jr:detailCell style="table 2_TD" height="20">
<textField>
<reportElement uuid="5c95b1c4-bfb0-4364-83da-28d78e2d0555" x="0" y="0" width="90" height="20"/>
<textElement/>
<textFieldExpression><![CDATA[""+$F{tableData}]]></textFieldExpression>
</textField>
</jr:detailCell>
</jr:column>
<jr:column uuid="ec2f8c0c-e950-4b74-9d1e-851f68deedf3" width="90">
<jr:tableHeader style="table 2_TH" height="30"/>
<jr:detailCell style="table 2_TD" height="20"/>
</jr:column>
</jr:table>
</componentElement>
</band>
</detail>
这是我的 ProtoReport 类的初学者(getter、setter 等被切断):
public class ProtoReport {
private String outputFileName;
private String title;
private String logoLocation;
private String paragraphText;
private List<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 = null;
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\\logo.jpg";
picLocation = path + "\\reports\\pic.jpg";
}
//constructor
public ProtoReport(String title, String logoLocation,
String picLocation, ChartData chartData) {
// Initialize object fields
outputFileName = "PrototypeReport";
this.title = title;
paragraphText = "Default text";
//tableData = new ArrayList<String>();
tableData = null;
this.chartData = chartData;
//set path to working directory
path = System.getProperty("user.dir");
//picture locations
this.logoLocation = logoLocation;
this.picLocation = picLocation;
}
//Getters and Setters
//Self referencing method for use in iReport field description
public ProtoReport getMe()
{
return this;
}
最后,我的 Main 的相关部分:
public static void main(String[] args) {
List<ProtoReport> listOfReports = new ArrayList<ProtoReport>();
ProtoReport protoReport1 = new ProtoReport();
ProtoReport protoReport2 = new ProtoReport();
//test table.. since nothing else will work...
List<String> testTable = new ArrayList<String>();
testTable.add("First entry test");
testTable.add("Second entry test");
protoReport1.setTableData(testTable);
protoReport1.getTableData().add("First entry");
protoReport1.getTableData().add("Second entry");
listOfReports.add(protoReport1);
//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;
}
由于这与我在图表中遇到的问题相似,我怀疑它们的原因相同。我一直在阅读有关如何使用这些功能的所有内容,但只是没有点击...如何在 iReport 中设置连接以使用 tableData 中的字符串填充我的表格?我是否需要每列的字符串列表,或者是否有更聪明的方法来完成这一切(请记住,我想用 POJO 来完成这一切,即没有 sql 数据库)。