2017-03-27 60 views
-1

我想創建一個簡單的賈斯珀報告與1表應該由java代碼使用JRResultSetDataSource對象填充。但是,我無法將值傳遞給Table組件。賈斯珀報告如何將來自JRResultSetDataSource的數據設置爲表組件

我不尋找參數列表或JRBeanCollectionDataSource作爲解決方案。

我的報告JRXML:

<?xml version="1.0" encoding="UTF-8"?> 
<!-- Created with Jaspersoft Studio version 6.3.1.final using JasperReports Library version 6.3.1 --> 
<!-- 2017-03-27T17:04:35 --> 
<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="tab1" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="8a63304f-05d6-485c-82e7-5591159f68fb"> 
    <style name="Table_TH" mode="Opaque" backcolor="#F0F8FF"> 
     <box> 
      <pen lineWidth="0.5" lineColor="#000000"/> 
      <topPen lineWidth="0.5" lineColor="#000000"/> 
      <leftPen lineWidth="0.5" lineColor="#000000"/> 
      <bottomPen lineWidth="0.5" lineColor="#000000"/> 
      <rightPen lineWidth="0.5" lineColor="#000000"/> 
     </box> 
    </style> 
    <style name="Table_CH" mode="Opaque" backcolor="#BFE1FF"> 
     <box> 
      <pen lineWidth="0.5" lineColor="#000000"/> 
      <topPen lineWidth="0.5" lineColor="#000000"/> 
      <leftPen lineWidth="0.5" lineColor="#000000"/> 
      <bottomPen lineWidth="0.5" lineColor="#000000"/> 
      <rightPen lineWidth="0.5" lineColor="#000000"/> 
     </box> 
    </style> 
    <style name="Table_TD" mode="Opaque" backcolor="#FFFFFF"> 
     <box> 
      <pen lineWidth="0.5" lineColor="#000000"/> 
      <topPen lineWidth="0.5" lineColor="#000000"/> 
      <leftPen lineWidth="0.5" lineColor="#000000"/> 
      <bottomPen lineWidth="0.5" lineColor="#000000"/> 
      <rightPen lineWidth="0.5" lineColor="#000000"/> 
     </box> 
    </style> 
    <subDataset name="Empty Dataset1" uuid="be9c1aa9-031c-4232-a9f2-cdcb0a5bd17d"> 
     <field name="MONTH" class="java.lang.Integer"/> 
     <field name="YEAR" class="java.lang.Integer"/> 
     <field name="COUNT" class="java.lang.Integer"/> 
    </subDataset> 
    <queryString> 
     <![CDATA[]]> 
    </queryString> 
    <field name="MONTH" class="java.lang.Integer"/> 
    <field name="YEAR" class="java.lang.Integer"/> 
    <field name="COUNT" class="java.lang.Integer"/> 
    <background> 
     <band splitType="Stretch"/> 
    </background> 
    <title> 
     <band height="79" splitType="Stretch"/> 
    </title> 
    <pageHeader> 
     <band height="35" splitType="Stretch"/> 
    </pageHeader> 
    <columnHeader> 
     <band height="61" splitType="Stretch"/> 
    </columnHeader> 
    <detail> 
     <band height="360" splitType="Stretch"> 
      <componentElement> 
       <reportElement x="0" y="0" width="200" height="200" uuid="684e26d7-94cf-49cc-874d-104e29444537"> 
        <property name="com.jaspersoft.studio.layout" value="com.jaspersoft.studio.editor.layout.VerticalRowLayout"/> 
        <property name="com.jaspersoft.studio.table.style.table_header" value="Table_TH"/> 
        <property name="com.jaspersoft.studio.table.style.column_header" value="Table_CH"/> 
        <property name="com.jaspersoft.studio.table.style.detail" value="Table_TD"/> 
        <property name="net.sf.jasperreports.export.headertoolbar.table.name" value=""/> 
       </reportElement> 
       <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" whenNoDataType="Blank"> 
        <datasetRun subDataset="Empty Dataset1" uuid="0baea769-1082-4618-9eee-520f59145c07"> 
         <connectionExpression><![CDATA[]]></connectionExpression> 
        </datasetRun> 
        <jr:column width="60" uuid="ba2a25da-f872-4a99-8703-9e6e15fc988a"> 
         <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/> 
         <jr:tableHeader style="Table_TH" height="30" rowSpan="1"> 
          <staticText> 
           <reportElement x="0" y="0" width="60" height="30" uuid="a4bbf819-044c-47ff-bff0-627859b51444"/> 
           <text><![CDATA[MONTH]]></text> 
          </staticText> 
         </jr:tableHeader> 
         <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/> 
         <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/> 
         <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/> 
         <jr:detailCell style="Table_TD" height="30"> 
          <textField> 
           <reportElement x="0" y="0" width="60" height="30" uuid="91cd6b4f-f09b-4c08-8fe7-0645468bd1cc"/> 
           <textFieldExpression><![CDATA[$F{MONTH}]]></textFieldExpression> 
          </textField> 
         </jr:detailCell> 
        </jr:column> 
        <jr:column width="70" uuid="80e850bb-b112-4b53-aa44-f9fca76710b4"> 
         <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/> 
         <jr:tableHeader style="Table_TH" height="30" rowSpan="1"> 
          <staticText> 
           <reportElement x="0" y="0" width="70" height="30" uuid="86e7d6fb-ee79-4c11-8e3d-99b59832b978"/> 
           <text><![CDATA[YEAR]]></text> 
          </staticText> 
         </jr:tableHeader> 
         <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/> 
         <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/> 
         <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/> 
         <jr:detailCell style="Table_TD" height="30"> 
          <textField> 
           <reportElement x="0" y="0" width="70" height="30" uuid="39a27fc4-79d6-4ff8-aae0-ff67ac43bd09"/> 
           <textFieldExpression><![CDATA[$F{YEAR}]]></textFieldExpression> 
          </textField> 
         </jr:detailCell> 
        </jr:column> 
        <jr:column width="70" uuid="57949a99-6a7c-4911-b01c-1db16d3963bd"> 
         <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column3"/> 
         <jr:tableHeader style="Table_TH" height="30" rowSpan="1"> 
          <staticText> 
           <reportElement x="0" y="0" width="70" height="30" uuid="cddf1b17-b113-4cc5-a95c-395f59d96181"/> 
           <text><![CDATA[COUNT]]></text> 
          </staticText> 
         </jr:tableHeader> 
         <jr:tableFooter style="Table_TH" height="30" rowSpan="1"/> 
         <jr:columnHeader style="Table_CH" height="30" rowSpan="1"/> 
         <jr:columnFooter style="Table_CH" height="30" rowSpan="1"/> 
         <jr:detailCell style="Table_TD" height="30"> 
          <textField> 
           <reportElement x="0" y="0" width="70" height="30" uuid="fefdadb9-d47e-45b5-9aa4-346c53ce0441"/> 
           <textFieldExpression><![CDATA[$F{COUNT}]]></textFieldExpression> 
          </textField> 
         </jr:detailCell> 
        </jr:column> 
       </jr:table> 
      </componentElement> 
     </band> 
     <band height="50"/> 
    </detail> 
    <columnFooter> 
     <band height="45" splitType="Stretch"/> 
    </columnFooter> 
    <pageFooter> 
     <band height="54" splitType="Stretch"/> 
    </pageFooter> 
    <summary> 
     <band height="42" splitType="Stretch"/> 
    </summary> 
</jasperReport> 

我的Java代碼片斷:

resultSet = statement.executeQuery(query); 

if(resultSet != null && reportStream !=null) 
{    
JRResultSetDataSource jrr = new JRResultSetDataSource(resultSet); 
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream,new HashMap(),jrr); 
} 

到目前爲止,我可以列出報表上的數據,如果我不使用Table組件,而不是僅僅把報告中的字段。 但我無法找到一種方法來推送表組件中的數據,因爲它有一個單獨的數據源和相應的字段組件。

這種使用數據源填充表數據的方式甚至可能嗎? 有人可以舉個例子,或者指點我一個例子。

感謝,
Vishalendu

+0

這也許是有用的http:// stackoverflow.com/questions/11018651/can-i-use-data-from-jtable-for-my-jasper -report –

回答

1

你需要做這樣的事情:

在JRXML

<subDataset name="Empty Dataset1" ...> 
... 
</subDataset> 
<parameter name="TableDataSource" class="net.sf.jasperreports.engine.JRDataSource"/> 
... 
<jr:table ...> 
    <datasetRun subDataset="Empty Dataset1" ...> 
     <dataSourceExpression><![CDATA[$P{TableDataSource}]]></dataSourceExpression> 
    </datasetRun> 
... 

在Java

JRResultSetDataSource jrr = new JRResultSetDataSource(resultSet); 
Map params = new HashMap(); 
params.put("TableDataSource", jrr); 
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, params, new JREmptyDataSource()); 
+0

在修改jrxml(並生成更新的.jasper)之後,將HashMap添加到JasperRunManager.runReportToPdfStream()方法中,我得到了ClassCastException:java.lang.ClassCastException:net.sf.jasperreports.engine.JRResultSetDataSource與java不兼容.sql.Connection。如果我刪除了你的HashMap,表格是空白的,但沒有錯誤。 (完整堆棧跟蹤:https://pastebin.com/yAsnsNiN) – Vishalendu

+0

請發佈您修改的JRXML和填充異常stracktrace。 PS:我會看到你的改變和異常stacktrace的一個瘋狂猜測是你有<![CDATA [$ P {TableDataSource}]]>而不是<! [CDATA [$ P {TableDataSource}]]>在您的JRXML中。 – dada67

+0

謝謝,你已經發現了。我已經犯了上面解釋過的同樣的錯誤。此解決方案有效。我還有一個問題,您認爲有一種使用報表設計器的方式,即可以實現此解決方案而不是通過手動編碼? – Vishalendu