2014-07-20 86 views
0

我想通過並使用iReport設計器打印動態二維數組對象。無法設計/打印2D陣列

我簡單的數據源是如下(這是動態的,具有N行和列實時):

Object [][] myData = new Object [][] {{"RECORD1"}, {"RECORD2"}}; 

我把它傳遞給JasperDataSourceBuilder類:

我已經寫了下面的代碼相同:

import net.sf.jasperreports.engine.JRDataSource; 
import net.sf.jasperreports.engine.JRException; 
import net.sf.jasperreports.engine.JRField; 

public class JasperDataSourceBuilder implements JRDataSource { 

    private static int rowIndex = 0; 
    private static int columnIndex = -1; 

    private Object[][] data = null; 

    public JasperDataSourceBuilder(Object[][] dataObject){ 
     data = dataObject; 
    } 

    @Override 
    public boolean next() throws JRException { 
     boolean hasMore = false; 
     if((data != null && data.length > 0)){ 
      if(rowIndex < data.length){ 
       columnIndex++; 
       if(columnIndex < data[rowIndex].length){ 
        hasMore = true; 
       }else{ 
        rowIndex ++; 
        columnIndex = 0; 
        hasMore = true; 
       } 
      } 
      } 
     return hasMore; 
    } 

    @Override 
    public Object getFieldValue(JRField arg0) throws JRException { 
     return String.valueOf(data[rowIndex][columnIndex]); 
    } 

    public Object[][] getData() { 
     return data; 
    } 

    public void setData(Object[][] data) { 
     this.data = data; 
    } 

} 

當我打電話給我的report()方法生成的PDF,但只有第一行被打印在報告中。

我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="report2" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="4f311e4c-b629-4c6c-a718-451aec9c679e"> 
    <style name="table"> 
     <box> 
      <pen lineWidth="1.0" lineColor="#000000"/> 
     </box> 
    </style> 
    <style name="table_TH" mode="Opaque" backcolor="#A9A9A9"> 
     <box> 
      <pen lineWidth="0.5" lineColor="#000000"/> 
     </box> 
    </style> 
    <style name="table_CH" mode="Opaque" backcolor="#FFBFBF"> 
     <box> 
      <pen lineWidth="0.5" lineColor="#000000"/> 
     </box> 
    </style> 
    <style name="table_TD" mode="Opaque" backcolor="#FFFFFF"> 
     <box> 
      <pen 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="#BBA8A8"> 
     <box> 
      <pen lineWidth="0.5" lineColor="#000000"/> 
     </box> 
    </style> 
    <style name="table 1_CH" mode="Opaque" backcolor="#FFE6E6"> 
     <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="#FFF8F8"/> 
     </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> 
    </style> 
    <subDataset name="dataset1" uuid="15c5b3b7-2873-4678-a6ad-67db2344acaa"/> 
    <field name="field1" class="java.lang.String"> 
     <fieldDescription><![CDATA[RECORD HEADER]]></fieldDescription> 
    </field> 
    <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"> 
      <staticText> 
       <reportElement x="94" y="2" width="100" height="20" uuid="cdfaad19-02d4-4846-9b06-c71a7df3c92f"/> 
       <text><![CDATA[RECORD HEADER]]></text> 
      </staticText> 
     </band> 
    </columnHeader> 
    <detail> 
     <band height="125"> 
      <textField> 
       <reportElement x="94" y="55" width="100" height="20" uuid="d154b00a-f10d-4803-9901-4580080fc982"/> 
       <textFieldExpression><![CDATA[$F{field1}]]></textFieldExpression> 
      </textField> 
     </band> 
    </detail> 
    <columnFooter> 
     <band height="45" splitType="Stretch"/> 
    </columnFooter> 
    <pageFooter> 
     <band height="54" splitType="Stretch"/> 
    </pageFooter> 
    <summary> 
     <band height="42" splitType="Stretch"/> 
    </summary> 
</jasperReport> 

我想在我的應用程序,其中目前我使用itextPDF凡在我的數據是所有二維物體顯示頁眉&數據表使用JasperReports的。

我想保留Object[][]作爲數據源並在我現有的報告中實現它。

+0

我在 - http://jasperreports.sourceforge.net/sample.reference/datasource/#customdatasource找到了一個參考 – pshinde31

回答

0

我可以成功使用Object [] []作爲數據源。

在下面的類中,我遍歷我的2D對象&返回值。 getFieldValue方法僅從2D對象的下一個索引返回正確的值。 (請確保匹配沒有在JRXML列字段與您的2D對象)

public class JasperDataSourceBuilder implements JRDataSource { 

    private int rowIndex = 0; 
    private int columnIndex = 0; 

    private Object[][] data = null; 

    public JasperDataSourceBuilder(Object[][] dataObject){ 
     data = dataObject; 
    } 

    @Override 
    public boolean next() throws JRException { 
     boolean hasMore = false; 
     if((data != null && data.length > 0)){ 
      if(rowIndex < data.length){ 
       if(columnIndex < data[rowIndex].length){ 
        hasMore = true; 
       }else{ 
        rowIndex ++; 
        columnIndex = 0; 
        hasMore = true; 
       } 
      } 
      } 
     if(rowIndex >= data.length){ 
      hasMore = false; 
     } 
     return hasMore; 
    } 

    @Override 
    public Object getFieldValue(JRField arg0) throws JRException { 
     String fieldVal = String.valueOf(data[rowIndex][columnIndex]); 
     columnIndex++; 
     return fieldVal; 
    } 

    public Object[][] getData() { 
     return data; 
    } 

    public void setData(Object[][] data) { 
     this.data = data; 
    } 

} 

然後生成報告通過使用下面的代碼 -

jasperPrint = JasperFillManager.fillReport(
     reportStream, parameters, new JasperDataSourceBuilder(dataObject)); 
String fileName = "report2.PDF"; 
response.setContentType("application/pdf;charset=ISO-8859-1"); 
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); 
response.setHeader("Cache-Control", "max-age=0"); 
OutputStream ouputStream = response.getOutputStream(); 
JasperExportManager.exportReportToPdfStream(jasperPrint, ouputStream); 
ouputStream.flush(); 
ouputStream.close(); 

以上就可以實現通過擴展AbstractTableModel上也是如此,但對於我的要求&簡單我將實施JRDataSource如上。

我發現上面評論中的參考鏈接非常有用。

希望這有助於一些新的JasperReports。

謝謝!