在 iReport 中,您可以不创建不同月份的静态列表(组合框),您只能提示插入参数。
用户需要手动输入 3,6 ecc。
<parameter name="parameter1" class="java.lang.Integer" isForPrompting="true">
<defaultValueExpression><![CDATA[3]]></defaultValueExpression>
</parameter>
iReport 不是为您的客户使用而开发的,而是供您在开发报告时使用的。
如果您不想使用jasperserver,您可以开发自己的应用程序供用户选择数据 ecc。下面是javaswing 应用程序的示例,要求选择月份并生成报告预览(您需要修复连接设置并为 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);
}
}
当然你也可以开发类似的网络应用程序。