1

我编写了这段代码来使用iReport 4.7.1显示一个简单的报告。

  private void printReport() {
    try {
        JRTableModelDataSource dataSource = new JRTableModelDataSource(
                new JTable().getModel());

        String reportSource = "C:\\Reports\\report1.jrxml";
        Map<String, Object> params = new HashMap<String, Object>();

        params.put("Name", "SriLanka");

        JasperReport jasperReport = JasperCompileManager.compileReport(reportSource);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
        JasperViewer.viewReport(jasperPrint, false);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

这是我的图书馆:

  • commons-beanutils-1.8.0.jar
  • commons-collections-2.1.1.jar
  • commons-digester-2.1.jar
  • commons-logging-1.1.1.jar
  • jasperrreports-4.7.1.jar
  • jasperreports-applet-4.7.1.jar
  • jasperreports-javaflow-4.7.1.jar
  • groovy-all-1.7.5.jar

NetBeans输出显示此警告

2012 年 10 月 8 日下午 2:57:31 net.sf.jasperreports.engine.component.ComponentsEnvironment findBundles 警告:找到命名空间http://jasperreports.sourceforge.net/jasperreports/components的两个组件

这是.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="report1" language="groovy" pageWidth="595" pageHeight="842" whenNoDataType="BlankPage" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="5174594e-de3c-4b09-932b-c6665bf6a34b">
    <parameter name="Name" class="java.lang.String" isForPrompting="false"/>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch">
            <textField>
                <reportElement uuid="fd02a8bc-548b-41c1-aed5-2a7c2e257704" x="210" y="23" width="157" height="36"/>
                <textElement>
                    <font size="18"/>
                </textElement>
                <textFieldExpression><![CDATA[$P{Name}]]></textFieldExpression>
            </textField>
        </band>
    </title>
    <pageHeader>
        <band height="35" splitType="Stretch"/>
    </pageHeader>
    <columnHeader>
        <band height="61" splitType="Stretch"/>
    </columnHeader>
    <detail>
        <band height="125" splitType="Stretch"/>
    </detail>
    <columnFooter>
        <band height="45" splitType="Stretch"/>
    </columnFooter>
    <pageFooter>
        <band height="54" splitType="Stretch"/>
    </pageFooter>
    <summary>
        <band height="42" splitType="Stretch"/>
    </summary>
</jasperReport>

报告打开但不显示任何文本。

4

1 回答 1

1

To solve this problem you should:

  1. check your datasource, it can be empty;
  2. add textFields elements with fields expressions (<textFieldExpression><![CDATA[$F{field}]]></textFieldExpression>) to the Detail band to show records in report.

Anyway your parameter Name should be displayed in the Title band (in generated report). You can pass even the JREmptyDataSource to check this.


For showing data you can chose one of this ways:

1. Using Detail band

It is classical, "old school" method for showing data stored in records from the data source. The JR engine iterates through the data source for rendering this section. With help of textField elements we can show the fields values.

The sample: look at $jasperreports$\demo\samples\jasper sample from JR distribution package.

2. Using Crosstab component

The Crosstab component used for summarizing data into grid which has two dimensions.

The sample: look at $jasperreports$\demo\samples\crosstabs sample.

3. Using List component

The List component iterates on set of its records (defined with help of subDataset property).

The sample: look at Using the Built-in List Component sample.

4. Using Table component

The Table is using for showing data with tabular structure. As a List component has its own subDataset.

The sample: look at Using the Built-in Table Component sample.

4. Using Subreport component

The Subreport is using for building complex reports such as compilation of different reports in one. This component often operates with subqueries.

The sample: look at Subreports sample.


You can read details in JasperReports Ultimate Guide

于 2012-10-09T07:18:24.067 回答