在 Jaspersoft ireport 中创建静态下拉列表

Creating static drop down list in Jaspersoft ireport

我正在 "Jaspersoft iReport designer 5.6.0" 中创建报告。我想要做的是添加一个参数(一个静态列表,其中包含以下值:6 个月、3 个月...)。当用户选择这些选项之一时,我应该能够在 Report 查询中获取用户选择的值,这样我就可以根据该选择提供结果。我不想使用 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}

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

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

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

iReport 不是为了给您的客户使用而开发的,而是为了您在开发报表时使用。

如果您不想使用 , you can develop your own application for user to select data ecc. Below you find an example of a 应用程序,要求 select 个月并生成报告预览(您需要修复连接设置并为 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);
  }
}

当然你也可以开发类似的web应用。