【问题标题】:Creating static drop down list in Jaspersoft ireport在 Jaspersoft ireport 中创建静态下拉列表
【发布时间】:2018-06-14 18:18:05
【问题描述】:

我正在“Jaspersoft iReport Designer 5.6.0”中创建报告。我想要做的是添加一个参数(一个包含值的静态列表,例如:6 个月、3 个月......)。当用户选择其中一个选项时,我应该能够在报告查询中获取用户选择的值,以便我可以根据该选择提供结果。我不想使用 jasper 服务器。

这可能吗?

这是我的查询:

SELECT
     DISTRICT."DKEY" AS DISTRICT_DKEY,
     DISTRICT."PROVINCE_ID" AS DISTRICT_PROVINCE_ID,
     DISTRICT."DISTRICT" AS DISTRICT_DISTRICT
     DISTRICT."DURATION" AS DISTRICT_DURATION
FROM
     "dbo"."DISTRICT" DISTRICT
where DISTRICT."DKEY" = $P{parameter1}

【问题讨论】:

    标签: jasper-reports prompt ireport


    【解决方案1】:

    在 iReport 中,您可以创建不同月份的静态列表(组合框),您只能提示插入参数。

    用户需要手动输入 3,6 ecc。

    <parameter name="parameter1" class="java.lang.Integer" isForPrompting="true">
        <defaultValueExpression><![CDATA[3]]></defaultValueExpression>
    </parameter>
    

    iReport 不是为您的客户使用而开发的,而是供您在开发报告时使用的。

    如果您不想使用,您可以开发自己的应用程序供用户选择数据 ecc。下面是 应用程序的示例,要求选择月份并生成报告预览(您需要修复连接设置并为 jrxml 文件提供正确的路径):

    import java.awt.*;
    import java.awt.event.*;
    import java.sql.Connection;
    import java.util.*;
    import javax.swing.*;
    import net.sf.jasperreports.engine.*;
    import net.sf.jasperreports.engine.design.JasperDesign;
    import net.sf.jasperreports.engine.xml.JRXmlLoader;
    import net.sf.jasperreports.view.JRViewer;
    
    public class JasperReportInterface extends JFrame {
    
      private static final long serialVersionUID = 5430239481089683268L;
      private JComboBox<MonthItem> selectMonts;
    
      public JasperReportInterface() {
        super("Jasper Report Interface");
        jbInit();
      }
    
      private void jbInit() {
        this.getContentPane().setLayout(new GridBagLayout());
    
        selectMonts = new JComboBox<MonthItem>();
        selectMonts.addItem(new MonthItem(3));
        selectMonts.addItem(new MonthItem(6));
    
        this.getContentPane().add(new JLabel("Select month:"), new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(2, 2, 2,
                2), 0, 0));
        this.getContentPane().add(selectMonts, new GridBagConstraints(1, 0, 1, 1, 1.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.HORIZONTAL, new Insets(2, 2, 2,
                2), 0, 0));
    
        JButton btnReport = new JButton("Generate report");
        btnReport.addActionListener(new ActionListener() {
    
            @Override
            public void actionPerformed(ActionEvent e) {
                btnReport_actionPerformed(e);
            }
        });
    
        this.getContentPane().add(btnReport,new GridBagConstraints(0, 1, 2, 1, 1.0, 1.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(2, 2, 2,
                2), 0, 0));
      }
    
      protected void btnReport_actionPerformed(ActionEvent e) {
    
        String jasperFilePath = "jasper/myJasperFile.jrxml";
    
        Map<String, Object> parameters = new HashMap<String, Object>();
        Object v = selectMonts.getSelectedItem();
        if (v instanceof MonthItem) {
            parameters.put("parameter1", ((MonthItem) v).getMonth());
        }
    
        Connection conn = null; // Pass the connection to database or datasource
    
        JasperPrint report;
        try {
            JasperDesign jasperDesign = JRXmlLoader.load(jasperFilePath);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            report = JasperFillManager.fillReport(jasperReport, parameters, conn);
        } catch (JRException e1) {
            e1.printStackTrace();
            JOptionPane.showMessageDialog(this, "Error creating report: " + e1.getMessage());
            return;
        }
    
        JRViewer jrv = new JRViewer(report);
        JDialog viewer = new JDialog(this);
        viewer.setTitle("Print preview");
        viewer.getContentPane().add(jrv);
        viewer.pack();
        viewer.setSize(new Dimension(840, 600));
        viewer.setLocationRelativeTo(null);
        viewer.setVisible(true);
      }
    
      class MonthItem {
        private int month;
        protected MonthItem(int month) {
            this.month = month;
        }
        public String toString() {
            return month + " months";
        }
        public int getMonth() {
            return month;
        }
      }
    
      public static void main(String[] args) {
        JasperReportInterface ri = new JasperReportInterface();
        ri.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ri.setSize(400,100);
        ri.setLocationRelativeTo(null);
        ri.setVisible(true);
      }
    }
    

    当然你也可以开发类似的网络应用程序。

    【讨论】:

    • 亲爱的,我最终使用 extjs 4.2 构建了一个表单,并根据用户的参数在网格中显示选定的记录。但是如何将网格连接到 jasper ireport?
    • @mikeb 您并没有真正将其连接到 ireport(ireport 只是开发报告的工具,jrxml)...我需要查看您的网格以准确了解您需要什么,但通常是这个过程正在创建一个 JRDatasource(基于您的参数),调用 jrxml(报告)并导出为您喜欢的格式(pdf、excel、html ecc)。在报告中,如果列是动态的,您应该考虑使用交叉表。如果您通过数据源示例传递另一个问题,期望的布局 jrxml ecc,,我可以尝试帮助您。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 2021-05-18
    相关资源
    最近更新 更多