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

时间:2021-12-31 11:13:32

I already have 6 Jasper Report templates created, with all of the static text fields to be filled out using a SQL query. The SQL query uses 2 parameters that I am passing in: FirstName and LastName. I am also passing in 2 other parameters that will just get added to Report.

我已经创建了6个Jasper Report模板,所有静态文本字段都使用SQL查询填写。 SQL查询使用我传入的2个参数:FirstName和LastName。我还传递了其他2个参数,这些参数只会添加到Report中。

This is the code i have so far to pass the HashMap with the parameters to the Report. But I am lost as there isnt really any good documentation or examples that I can find.

这是我到目前为止将带有参数的HashMap传递给Report的代码。但是我很遗憾,因为我找不到任何好的文档或示例。

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);
        }
    }
  }
}

I know this is probably far from correct, but if someone can point me in the right direction of good documentation or examples, or point out what I've done wrong, it would be of great help!

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

1 个解决方案

#1


3  

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

以下是将Jasper Report与Java Application一起使用的简要说明

  • First you have to set database connection to the Report.
  • 首先,您必须设置与报告的数据库连接。

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

  • Then you can design your SQL query for the report.

    如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用 You can add parameters to the SQL query in case you need to filter data through your query.Just add parameters using New Parameter button and you can drag and drop parameters displaying inside the text area to your query.
  • 然后,您可以为报表设计SQL查询。如果需要通过查询过滤数据,可以向SQL查询添加参数。只需使用“新建参数”按钮添加参数,就可以将显示在文本区域内的参数拖放到查询中。

In your report inspector you can see all the column names of our query under Fields category and all the parameters defined under Parameters category.

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

在报表检查器中,您可以在“字段”类别下查看查询的所有列名称,并在“参数”类别下查看所有参数。

  • You can design a basic report like below

    By dragging and dropping field names and parameters to your Report Designer you can design your report as you desire. Title Band(Your Title Here) , Column Header Band(Column Names) , Detail Band(Iterative Data here) and Summary Band(Like Grand_Total,Date,Issued By and chart elements can be added to this section) are enough for basic report.

    如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用
  • 您可以设计如下的基本报告通过将字段名称和参数拖放到报表设计器,您可以根据需要设计报表。标题带(您的标题在此处),列标题带(列名称),详细信息带(此处为迭代数据)和摘要带(如Grand_Total,日期,发布者和图表元素可添加到此部分)足以用于基本报告。
  • Then you can declare ReportGenarator class to generate your report

    然后,您可以声明ReportGenarator类以生成报告

    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();   
        }
    } }
    
  • You can generate your report by Pressing a button in your application.So that you have to include below code inside your button action event

    您可以通过按应用程序中的按钮生成报告。因此,您必须在按钮操作事件中包含以下代码

    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);
    

#1


3  

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

以下是将Jasper Report与Java Application一起使用的简要说明

  • First you have to set database connection to the Report.
  • 首先,您必须设置与报告的数据库连接。

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

  • Then you can design your SQL query for the report.

    如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用 You can add parameters to the SQL query in case you need to filter data through your query.Just add parameters using New Parameter button and you can drag and drop parameters displaying inside the text area to your query.
  • 然后,您可以为报表设计SQL查询。如果需要通过查询过滤数据,可以向SQL查询添加参数。只需使用“新建参数”按钮添加参数,就可以将显示在文本区域内的参数拖放到查询中。

In your report inspector you can see all the column names of our query under Fields category and all the parameters defined under Parameters category.

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

在报表检查器中,您可以在“字段”类别下查看查询的所有列名称,并在“参数”类别下查看所有参数。

  • You can design a basic report like below

    By dragging and dropping field names and parameters to your Report Designer you can design your report as you desire. Title Band(Your Title Here) , Column Header Band(Column Names) , Detail Band(Iterative Data here) and Summary Band(Like Grand_Total,Date,Issued By and chart elements can be added to this section) are enough for basic report.

    如何使用java将参数传递给JasperReport以便稍后在SQL查询中使用
  • 您可以设计如下的基本报告通过将字段名称和参数拖放到报表设计器,您可以根据需要设计报表。标题带(您的标题在此处),列标题带(列名称),详细信息带(此处为迭代数据)和摘要带(如Grand_Total,日期,发布者和图表元素可添加到此部分)足以用于基本报告。
  • Then you can declare ReportGenarator class to generate your report

    然后,您可以声明ReportGenarator类以生成报告

    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();   
        }
    } }
    
  • You can generate your report by Pressing a button in your application.So that you have to include below code inside your button action event

    您可以通过按应用程序中的按钮生成报告。因此,您必须在按钮操作事件中包含以下代码

    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);