如何使用 java 将参数传递给 JasperReport,以便稍后在 SQL 查询中使用

How to pass parameters to JasperReport with java to use later in SQL query

我已经创建了 6 个 Jasper Report 模板,所有静态文本字段都使用 SQL 查询填写。 SQL 查询使用我传入的 2 个参数:FirstNameLastName。我还传递了另外 2 个参数,它们将被添加到报告中。

这是我迄今为止将带有参数的 HashMap 传递给报表的代码。但是我迷路了,因为我真的找不到任何好的文档或示例。

package print;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import java.util.*;

public class PrintCertificate  
{   
   public PrintCertificate(String output, String certType, String firstName, String lastName, String confirmDate, String pastorName)
   {
    if(certType=="rci_eng")
    {
        String fileName = "/RCI_Eng.jasper";
        output = "C:/Users/User/Desktop/Test/";

        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("FirstName",firstName);
        map.put("LastName",lastName);
        map.put("PastorName", pastorName);
        map.put("DateOfConfirmation", confirmDate);
        try
        {
            JasperPrint print = JasperFillManager.fillReport(fileName, map);
            JRDocxExporter exporter = new JRDocxExporter();
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, "test.docx");
            exporter.exportReport(print);

        }
        catch (Exception e)
        {
            e.printStackTrace();
            System.exit(1);
        }
    }
  }
}

我知道这可能远非正确,但如果有人能指出我正确的文档或示例方向,或者指出我做错了什么,那将有很大帮助!

Here is a brief description to use Jasper Report with your Java Application

  • 首先您必须设置与报表的数据库连接。

  • 然后您可以为报告设计 SQL 查询。

    您可以向 SQL 查询添加参数,以防您需要使用 新参数 按钮通过 query.Just 添加参数来过滤数据,您可以拖放参数显示在您查询的文本区域内。

在您的报告检查器中,您可以在 Fields 类别下看到我们查询的所有列名称,以及在 Parameters 类别下定义的所有参数.

  • 您可以像下面这样设计一个基本报表

    通过将字段名称和参数拖放到 Report Designer 中,您可以根据需要设计报表。 Title Band(您的标题在这里),Column Header Band(Column Names),Detail Band(此处为迭代数据)和汇总带(如Grand_Total、日期、发布者和图表元素可以添加到此部分)足以用于基本报告。

  • 然后你可以声明ReportGenarator class来生成你的 报告

    public class ReportGenarator {
    
    public static String OUT_PUT = "your_output_file_path/myreport.docx";
    public static String REPORT = "your_report_path/myreport.jrxml";
    
    public void genarateReport(String reportPath,
            Map<String, Object> map, Connection con) {
        try {
    
            JasperReport jr = JasperCompileManager.compileReport(
                    ClassLoader.getSystemResourceAsStream(reportPath));
            JasperPrint jp = JasperFillManager.fillReport(jr, map, con);
            JRDocxExporter export = new JRDocxExporter();
        export.setExporterInput(new SimpleExporterInput(jp));
        export.setExporterOutput(new SimpleOutputStreamExporterOutput(new File(OUT_PUT)));
        SimpleDocxReportConfiguration config = new SimpleDocxReportConfiguration();
        export.setConfiguration(config);
        export.exportReport();
        } catch (JRException ex) {
            ex.printStackTrace();   
        }
    } }
    
  • 您可以通过按下 application.So 中的按钮来生成报告,您必须在按钮中包含以下代码 动作事件

    Map<String, Object> map = new HashMap<>();
                map.put("headding", "REPORT FROM DATABASE CONNECTION");//parameter name should be like it was named inside your report.
                new ReportGenarator().genarateReport(
                        ReportGenarator.REPORT, map, your_DB_connction_reference_here);