到iReport的jTableDataSourceModel数据链接为空

时间:2018-11-14 10:33:22

标签: java jasper-reports

为了生成报告,我使用IReportDesigner。 将表格放到detailbend1中,仅包含2个列,并用字段参数填充列 在相应的表数据集“ Zeittab”字段中定义的COLUMN_0和COLUMN_1。

enter image description here

在Java中,代码应填充报告表:

DefaultTableModel tazeit = (DefaultTableModel)jTable2.getModel();        
JasperReport jasperReport;
JasperPrint jasperPrint;

HashMap<String, Object> parameter = new HashMap<String, Object>();

parameter.put("maschine", jTextField7.getText());

try {           
    jasperReport = JasperCompileManager.compileReport("jrxml/reporttest02.jrxml");
    jasperPrint = JasperFillManager.fillReport(jasperReport, parameter, new JRTableModelDataSource(jTable2.getModel()));
    JasperViewer.viewReport(jasperPrint, true);
}
catch (JRException e) {
      e.printStackTrace();
}

但是它在两个表中只报告为空。

IReportDesigner中表的xml部分:

    <detail>
    <band height="30" splitType="Stretch">
        <elementGroup/>
        <componentElement>
            <reportElement x="20" y="0" width="279" height="30" uuid="16877d28-e650-4f23-9087-937a5f072a21">
                <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"/>
            </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">
                <datasetRun subDataset="Zeittab" uuid="162a78f5-b35f-4eb3-bfc2-73fe9c00adb4">
                    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
                </datasetRun>
                <jr:column width="130" uuid="b25fe72b-f6c7-4a18-add1-50e4a9e951a6">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column1"/>
                    <jr:detailCell style="Table_TD" height="30">
                        <textField>
                            <reportElement x="0" y="0" width="130" height="30" uuid="e335c7d7-bbb4-4ea9-adfd-b6bfae26ac9e"/>
                            <textFieldExpression><![CDATA[$F{COLUMN_0}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
                <jr:column width="150" uuid="3fc1a8fc-5245-4980-90c4-a27dbe5f46fa">
                    <property name="com.jaspersoft.studio.components.table.model.column.name" value="Column2"/>
                    <jr:detailCell style="Table_TD" height="30">
                        <textField>
                            <reportElement x="0" y="0" width="150" height="30" uuid="1d6321c6-f2b9-43e7-8814-bd13404cfd79"/>
                            <textFieldExpression><![CDATA[$F{COLUMN_1}]]></textFieldExpression>
                        </textField>
                    </jr:detailCell>
                </jr:column>
            </jr:table>
        </componentElement>
    </band>
</detail>

问题: 我应该在“ Zeittab”中插入参数吗?

JRDataSource ds = new JRTableModelDataSource(jTable2.getModel());
parameter.put("Zeittab", ds);

我尝试过,但是报告中也没有null。

Netbeans中没有错误消息可以提示。

也许是由于XML中的空JREmptyDatasource造成的:

<datasetRun subDataset="Zeittab" uuid="162a78f5-b35f-4eb3-bfc2-73fe9c00adb4">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.JREmptyDataSource()]]></dataSourceExpression>
                </datasetRun>

如何解决jTable与报表中cols之间的数据链接?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。 我用一个3列的表格来解释它,这些列分别名为material,anzahl,info。

首先在jasper xml文件中声明,初始化参数名称:

<parameter name="MaterialCollection" class="java.util.Collection"/>

然后在jasper xml文件中进行表声明:

<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="MaterialDataset" uuid="ac99630e-03c5-41d1-a0a5-a054a4ffc607">
    <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{MaterialCollection})]]></dataSourceExpression>
</datasetRun>
<jr:column width="227" uuid="9a486baa-c231-461d-a816-b49e69d0e287">
    <jr:columnHeader style="table 15_CH" height="14" rowSpan="1">
        <staticText>
            <reportElement x="3" y="0" width="224" height="14" uuid="1fa2f252-3900-47db-9026-277847285dd4"/>
            <textElement textAlignment="Center" verticalAlignment="Middle">
                <font size="7" isBold="true"/>
            </textElement>
            <text><![CDATA[Material]]></text>
        </staticText>
    </jr:columnHeader>
    <jr:detailCell style="table 15_TD" height="12" rowSpan="1">
        <textField>
            <reportElement x="3" y="0" width="224" height="12" uuid="1a6bf583-a396-49cb-8c36-72fb19c83440"/>
            <textElement verticalAlignment="Middle">
                <font size="7"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{material}]]></textFieldExpression>
        </textField>
    </jr:detailCell>
</jr:column>
<jr:column width="36" uuid="dea2ad4c-6756-488e-90e7-053d2d3430be">
    <jr:columnHeader style="table 15_CH" height="14" rowSpan="1">
        <staticText>
            <reportElement mode="Transparent" x="0" y="0" width="36" height="14" forecolor="#000000" backcolor="#FFFFFF" uuid="234a9f2c-c163-4c77-af3a-cac70c258153"/>
            <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None" markup="none">
                <font fontName="SansSerif" size="7" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                <paragraph lineSpacing="Single"/>
            </textElement>
            <text><![CDATA[Anzahl]]></text>
        </staticText>
    </jr:columnHeader>
    <jr:detailCell style="table 15_TD" height="12" rowSpan="1">
        <textField pattern="" isBlankWhenNull="false">
            <reportElement mode="Transparent" x="0" y="0" width="36" height="12" forecolor="#000000" backcolor="#FFFFFF" uuid="b7924b73-a0f5-45d7-b175-ece4bdf65799"/>
            <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None" markup="none">
                <font fontName="SansSerif" size="7" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                <paragraph lineSpacing="Single"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{anzahl}]]></textFieldExpression>
        </textField>
    </jr:detailCell>
</jr:column>
<jr:column width="271" uuid="a098e0f8-8447-413d-8436-cb01dbaebb85">
    <jr:columnHeader style="table 15_CH" height="14" rowSpan="1">
        <staticText>
            <reportElement mode="Transparent" x="3" y="0" width="268" height="14" forecolor="#000000" backcolor="#FFFFFF" uuid="9d00320e-69cf-4d06-a099-e7c3c3c91344"/>
            <textElement textAlignment="Center" verticalAlignment="Middle" rotation="None" markup="none">
                <font fontName="SansSerif" size="7" isBold="true" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                <paragraph lineSpacing="Single"/>
            </textElement>
            <text><![CDATA[Info]]></text>
        </staticText>
    </jr:columnHeader>
    <jr:detailCell style="table 15_TD" height="12" rowSpan="1">
        <textField pattern="" isBlankWhenNull="false">
            <reportElement mode="Transparent" x="3" y="0" width="268" height="12" forecolor="#000000" backcolor="#FFFFFF" uuid="c3101aa2-53ff-4ffe-99ca-b29232bfb6fc"/>
            <textElement textAlignment="Left" verticalAlignment="Middle" rotation="None" markup="none">
                <font fontName="SansSerif" size="7" isBold="false" isItalic="false" isUnderline="false" isStrikeThrough="false" pdfEncoding="Cp1252" isPdfEmbedded="false"/>
                <paragraph lineSpacing="Single"/>
            </textElement>
            <textFieldExpression><![CDATA[$F{info}]]></textFieldExpression>
        </textField>
    </jr:detailCell>
</jr:column>

在Java代码中,我使用jasper xml声明中的列名称创建一个类:

public class Materialtabelle implements Serializable {
  public  String material;
  public  String anzahl;
  public  String info;

  public String getMaterial(){ return material; }
  public void setMaterial(String material){ this.material = material; }
  public String getAnzahl(){ return anzahl; }
  public void setAnzahl(String anzahl){ this.anzahl = anzahl; }
  public String getInfo(){ return info; }
  public void setInfo(String info){ this.info = info; }
}

然后我用名为ma的表数据填充它:

Materialtabelle matZeile = null;
    Collection matListe=new ArrayList<Materialtabelle>();
    for(int i = 0; i < jTable1.getRowCount(); i++){
        matZeile = new Materialtabelle();
        matZeile.setMaterial(((String)ma.getValueAt(i, 0)) == null ? " " : (String)ma.getValueAt(i, 0));
        matZeile.setAnzahl(((String)ma.getValueAt(i, 1)) == null ? " " : (String)ma.getValueAt(i, 1));
        matZeile.setInfo((String)ma.getValueAt(i, 2) == null ? " " : (String)ma.getValueAt(i, 2));
        matListe.add(matZeile);
   }

此后,我将数据放在报表的参数上:

HashMap<String, Object> parameter = new HashMap<String, Object>();
parameter.put("MaterialCollection", matListe);

然后调用创建jasperreport的标准,我使用jasperfile,这是一个已编译的jasper xml模板:

JasperReport jasperReport;
JasperPrint jasperPrint;

jasperReport = (JasperReport) JRLoader.loadObject(new File(syspfad + "reporttest01.jasper"));     
jasperPrint = JasperFillManager.fillReport(jasperReport, parameter, new JREmptyDataSource());
JasperViewer.viewReport(jasperPrint, false);