java 子报表的详细信息带未显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13024431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:12:22  来源:igfitidea点击:

Detail band of subreport not showing

javajasper-reportsireport

提问by Bnrdo

My main report contains 5 fields, 4 of them are of type java.lang.Stringand the last one is of type java.util.List. I used its latter as the data source for the subreport. I set the datasourceof the subreport.

我的主要报告包含 5 个字段,其中 4 个是java.lang.String类型,最后一个是java.util.List类型。我使用后者作为子报表的数据源。我设置了子报表数据源

Subreportproperties:

子报表属性:

Connection Type: Use a datasource expression
Datasource Expression: new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{Field5})

连接类型使用数据源表达式
数据源表达式new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{Field5})

Passing of data is working fine (I guess) because when I fill the report inside the Javaapplication. I can view the data passed to fields 1 to 4 but in field 5, at first, I can't verify because the subreportdoes not show data, only the static texts defined in the Column Header.

数据传递工作正常(我猜),因为当我在Java应用程序中填写报告时。我可以查看传递给字段 1 到 4 的数据,但在字段 5 中,起初我无法验证,因为子报表不显示数据,只显示Column Header中定义的静态文本。

Then when I placed the fields of the subreportin the Page Footer, I found out that the data is passed successfully, the problem is that the Detailband itself is not showing.

然后,当我把领域的报表页脚,我发现数据被顺利通过,问题在于细节乐队本身没有显示。

Why is it not showing?

为什么不显示?

In the subreportproperties, I have:

子报表属性中,我有:

When no data: All Sections, No Detail

无数据时所有部分,无详细信息

Please can anyone shed some light on this.

请任何人都可以对此有所了解。

回答by Raul Rene

I had the same problem for more than 1 day. In the application I am developing on there are multiple JasperReportswhich work. I encountered this problem when adding a new one. No matter what I tried, nothing displayed in the Detailband. I tried everything from triple-checking the Controller that populates the report, to upgrading to the latest jasperreportsjar and latest iReportsversion. Nothing seemed to work.

我遇到了同样的问题超过 1 天。在我正在开发的应用程序中,有多个JasperReports可以工作。我在添加新的时遇到了这个问题。无论我尝试什么,细节带中都没有显示任何内容。我尝试了从三重检查填充报告的控制器到升级到最新jasperreportsjar 和最新iReports版本的所有方法。似乎没有任何效果。

The problem is that the report is set by default to: When No Data: All Sections, No Detail, which basically means that if no data is sent to the report, it will display all sections, except for the Detailone.

问题是报表默认设置为: When No Data: All Sections, No Detail,这基本上意味着如果没有数据发送到报表,它将显示所有部分,除了详细信息

Although besides static text which I was using to test the report I was passing parameters directly from the Java Controller, it did not work until I added an EXEC [myFunction] $P{parameterId}to the Query Textproperty of the report. The function is a simple straightforward one, which takes a parameterIdpassed from Java as a parameter and returns something. (also, make sure you set the The language for the dataset query sqlproperty to SQL).

尽管除了我用来测试报告的静态文本之外,我还直接从 Java 控制器传递参数,但直到我向报告EXEC [myFunction] $P{parameterId}Query Text属性添加了 ,它才起作用。该函数是一个简单明了的函数,它将parameterId从 Java 传递的一个参数作为参数并返回一些内容。(另外,请确保将数据集查询的语言 sql属性设置为SQL)。

To sum up, for some reason the report doesn't seem to take the Java parameters as data (so it displays all the sections, except for Detail), so when I explicitly call a SQL function which returns some parameters and put them into my detail page, it works.

总而言之,由于某种原因,报告似乎没有将 Java 参数作为数据(因此它显示所有部分,除了Detail),所以当我显式调用一个 SQL 函数时,该函数返回一些参数并将它们放入我的详细信息页面,它的工作原理。

I hope this helps you, I busted my head for 10 hours to figure this out.

我希望这对你有帮助,我花了 10 个小时才弄清楚这一点。

回答by Alex K

You can try this sample.

你可以试试这个样本。

The POJO(Bean):

POJO(豆):

public class BeanWithList {

    private List<String> m_cities;
    private Integer m_id;

    public BeanWithList(List<String> cities, Integer id) {
        m_cities = cities;
        m_id = id;
    }

    public List<String> getCities() {
        return m_cities;
    }

    public Integer getId() {
        return m_id;
    }
}

The code to fill report:

填写报告的代码:

public static void testBuildPdf() {
    try {

        Map<String, Object> params = new HashMap<String, Object>();

        params.put("BeanSubreport", JasperCompileManager.compileReport(subreportSource));

        JasperReport jasperReport = JasperCompileManager.compileReport(masterSource);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, getDataSource());

        JasperExportManager.exportReportToPdfFile(jasperPrint, outputFileName);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private static JRDataSource getDataSource() {
    Collection<BeanWithList> coll = new ArrayList<BeanWithList>();

    BeanWithList bean = new BeanWithList(Arrays.asList("London", "Paris"), 1);

    coll.add(bean);

    bean = new BeanWithList(Arrays.asList("London", "Madrid", "Moscow"), 2);
    coll.add(bean);

    bean = new BeanWithList(Arrays.asList("Rome"), 3);
    coll.add(bean);

    return new JRBeanCollectionDataSource(coll);
}

The subreport jrxml:

子报表jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport .. leftMargin="0" rightMargin="0" topMargin="0" bottomMargin="0">
    <field name="city" class="java.lang.String">
        <fieldDescription><![CDATA[_THIS]]></fieldDescription>
    </field>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement positionType="Float" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{city}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

The master jrxml:

jrxml

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport ...>
    <parameter name="BeanSubreport" class="net.sf.jasperreports.engine.JasperReport"/>
    <field name="id" class="java.lang.Integer"/>
    <field name="cities" class="java.util.Collection"/>
    <title>
        <band height="103" splitType="Stretch">
            <staticText>
                <reportElement x="138" y="28" width="258" height="20"/>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true"/>
                </textElement>
                <text><![CDATA[Bean with List sample]]></text>
            </staticText>
        </band>
    </title>
    <columnHeader>
        <band height="20">
            <staticText>
                <reportElement x="0" y="0" width="100" height="20"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[Id]]></text>
            </staticText>
            <staticText>
                <reportElement x="100" y="0" width="100" height="20"/>
                <box>
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement textAlignment="Center" verticalAlignment="Middle">
                    <font isBold="true" isItalic="true" isUnderline="false"/>
                </textElement>
                <text><![CDATA[City name]]></text>
            </staticText>
        </band>
    </columnHeader>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement stretchType="RelativeToTallestObject" x="0" y="0" width="100" height="20"/>
                <box leftPadding="10">
                    <topPen lineWidth="1.0"/>
                    <leftPen lineWidth="1.0"/>
                    <bottomPen lineWidth="1.0"/>
                    <rightPen lineWidth="1.0"/>
                </box>
                <textElement/>
                <textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
            </textField>
            <subreport>
                <reportElement positionType="Float" stretchType="RelativeToTallestObject" x="100" y="0" width="100" height="20" isPrintWhenDetailOverflows="true"/>
                <dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{cities})]]></dataSourceExpression>
                <subreportExpression><![CDATA[$P{BeanSubreport}]]></subreportExpression>
            </subreport>
        </band>
    </detail>
</jasperReport>

The result will be:

结果将是:

The report in PDF format

PDF格式的报告



The main trick of this sample is the using of _THIS"variable".

此示例的主要技巧是使用_THIS“变量”。

For more information you can read JavaBean Data Sourcesarticle.

有关更多信息,您可以阅读JavaBean 数据源文章。

回答by Bnrdo

I solved it by setting the subreport parameter during runtime.

我通过在运行时设置子报表参数来解决它。

For those who encounter the same problem I just stored the JasperReport object (datatype is net.sf.jasperreports.engine.JasperReport) in a java variable then pass the variable to the parameter inside the report. This parameter is set as the value for Subreport Expression. Expression class of the subreport parameter should be net.sf.jasperreports.engine.JasperReport.

对于那些遇到同样问题的人,我只是将 JasperReport 对象(数据类型为net.sf.jasperreports.engine.JasperReport)存储在一个 java 变量中,然后将该变量传递给报告中的参数。此参数设置为Subreport Expression的值。子报表参数的表达式类应该是net.sf.jasperreports.engine.JasperReport.

回答by George Macus

When you pass parameters to FillManager, don't use null, use JREmptyDataSource.

给FillManager传参数的时候不要用null,用JREmptyDataSource。

Correct way: JasperFillManager.fillReport(this.getReportTemplate(JASPER_REPORT_EXPIRY_LETTER),this.parameters,new JREmptyDataSource());

正确方法: JasperFillManager.fillReport(this.getReportTemplate(JASPER_REPORT_EXPIRY_LETTER),this.parameters,new JREmptyDataSource());

Wrong way: JasperFillManager.fillReport(this.getReportTemplate(JASPER_REPORT_EXPIRY_LETTER),this.parameters, null);

错误方式:JasperFillManager.fillReport(this.getReportTemplate(JASPER_REPORT_EXPIRY_LETTER),this.parameters, null);

回答by Yuvraj Prajapati

import java.awt.Desktop;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.swing.JRViewer;
import net.sf.jasperreports.view.JasperViewer;

public class MyForm extends JFrame {



    public static JasperDesign jasperDesign;
    public static JasperPrint jasperPrint;
    public static JasperReport jasperReport;
    static JRBeanCollectionDataSource jrBeanCollectionDataSource;
    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                MyForm frame = new MyForm();
                frame.setVisible(true);
            }
        });
    }

    /**
     * Create the frame.
     */
    public MyForm() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 431, 286);
        setTitle("ThaiCreate.Com Java GUI Tutorial");
        getContentPane().setLayout(null);


        // Button Report
        JButton btnOpenReport = new JButton("Open Report");
        btnOpenReport.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                Connection connect = null;

                try {
                    Class.forName("com.mysql.jdbc.Driver");
                    connect =  DriverManager.getConnection("jdbc:mysql://localhost:3306/test" ,"root","");
                    System.out.println("Connected....");
                }
                catch (ClassNotFoundException e1) 
                {e1.printStackTrace();} 
                catch (SQLException e) 
                {e.printStackTrace();}

                // Application path
                FileInputStream report = null;
                try {
                     FileInputStream input = new FileInputStream(new File("C:\Users\admin\Desktop\report1.jrxml"));
                      try 
                     {

                        jasperDesign = JRXmlLoader.load(input);
                        jasperReport = JasperCompileManager.compileReport(jasperDesign);
                        jasperPrint = JasperFillManager.fillReport(jasperReport, null, connect);
                        JasperExportManager.exportReportToPdfFile(jasperPrint, "E:\demo.pdf");



//                      Desktop.getDesktop().open(new File("D:/ReceiptReport.pdf"));

                    } catch (JRException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } 



                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                JFrame frame = new JFrame();
                frame.setTitle("ThaiCreate.Com Customer Report");
                frame.setBounds(100, 100, 800,600);
                frame.getContentPane().add(new JRViewer(jasperPrint));
                frame.setVisible(true);

                try {
                    connect.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

        btnOpenReport.setBounds(137, 98, 146, 23);
        getContentPane().add(btnOpenReport);
        setResizable (false);
    }
}