首页 文章

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

提问于
浏览
2

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

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

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

1 回答

  • 4

    以下是将Jasper报告与Java应用程序一起使用的简要说明

    • First you have to set database connection to the Report.

    enter image description here

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

    enter image description here
    您可以在SQL查询中添加参数,以防需要通过查询过滤数据 . 只需使用 New Parameter 按钮添加参数,您就可以将显示在文本区域内的参数拖放到查询中 .

    在报表检查器中,您可以在 Fields 类别下查看查询的所有列名称,并在 Parameters 类别下查看所有参数 .

    enter image description here

    • You can design a basic report like below

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

    enter image description here

    • 然后您可以声明 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();   
        }
    } }
    
    • 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);
    

相关问题