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

web*_*r07 3 java parameters jasper-reports

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

这是我到目前为止将带有参数的 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);
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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

Mad*_*era 5

以下是在 Java 应用程序中使用 Jasper Report 的简要说明

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

在此处输入图片说明

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

    在此处输入图片说明 如果您需要通过查询过滤数据,您可以向 SQL 查询添加参数。只需使用“新建参数” 按钮添加参数,您就可以将显示在文本区域内的参数拖放到您的查询中。

在您的报告检查器中,您可以在“字段”类别下看到我们查询的所有列名称以及在“参数”类别下定义的所有参数。

在此处输入图片说明

  • 您可以设计如下的基本报告

    通过将字段名称和参数拖放到您的报告设计器中,您可以根据需要设计您的报告。Title Band (Your Title Here) , Column Header Band (Column Names) , Detail Band (Iterative Data here) 和Summary Band (像Grand_Total,Date,Issued By和图表元素可以添加到这个部分)对于基本的报告来说已经足够了。

    在此处输入图片说明
  • 然后你可以声明ReportGenerator类来生成你的报告

    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();   
        }
    } }
    
    Run Code Online (Sandbox Code Playgroud)
  • 您可以通过按下应用程序中的按钮来生成报告。因此您必须在按钮操作事件中包含以下代码

    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);
    
    Run Code Online (Sandbox Code Playgroud)