2012-09-23 10 views
3

我花了12小時的更多時間試圖弄清楚如何讓圖表和表格在碧玉中工作。這篇文章是關於表格(我的圖表問題,這可能是相同的問題,see here在iReports和Jasper和java bean數據源中使用表格功能

我想使用iReports調色板中的表格工具製作一個兩列的表。我有一個列表(以前是一個的ArrayList,但現在我想,因爲教程的一個列表我看着使用的,而不是和他們沒有問題)字符串名單內ProtoReport數據對象。 ProtoReport列表是我包裝在一個JRBeanCollectionDataSource並傳遞給JasperFill。 我希望tableData列表填充我的表中的一列。當我根據我的碧玉有限的經驗如何讓我認爲我應該,我碰到下面的錯誤此設置:

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,setters等切斷):

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; 
    } 

最後,我主要的相關部分:

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數據庫)。

回答

2

您在jrxml中提到的軟件包名稱錯誤。它是net.sf.jasperreports.engine.data.JRBeanCollectionDataSource

+0

你說得對,那是來自iReports中的自動填充 - 當你選擇詳細信息窗格中的連接/數據源實例**下的**使用數據源表達式**時,iReports的默認表達式是: ' new net.sf.jasperreports.engine.JREmptyDataSource(1)' 我將JREmpty更改爲** JRBeanCollectionDataSource **,但沒有注意到**。data **丟失。這是iReports 4.7.0中的錯誤嗎? 我會再次發佈一下,以驗證這解決了我的問題 – eljaydub

+0

更新:此修復程序糾正了我的編譯時錯誤。然後我不得不重新安排我的數據類型,因爲我無法獲得tableData的列表以直接將字符串打印到列中,同時也使用tableData作爲我的數據輸入源。我懷疑這一點,但想看看我是否可以擺脫它。最後,我寫了一個TableData類,它爲每列存儲一個列表,然後將列名作爲我的字段放在我的Table Data源下,然後放入ireports表中。 – eljaydub

相關問題