How to pass Date as parameter to jasper report

前端 未结 3 895
悲哀的现实
悲哀的现实 2020-12-20 09:49

I am trying to create JR report which is taking start_date and end_date as parameters.

The query:

SE         


        
相关标签:
3条回答
  • 2020-12-20 10:02

    Your code is wrong.

    You should pass parameters as below:

    Map<String, Object> map = new HashMap<String, Object>();
    map.put("frm_date", frm_dte);
    map.put("to_date", to_dte);
    

    You don't need to add P${} to the parameter's name.


    There are a lot of samples in JasperReports distribution package.

    You can look at this sample for more details.

    0 讨论(0)
  • 2020-12-20 10:02
    private JasperPrint generateReport() {
        Connection conn = null;
        JasperPrint myJPrint = null;
        try {
    
            conn =yourconnectionName;
    
            // parameters to be passed to the report
            Map<String, Object> params = new HashMap();
    
            // Loading my jasper file
            JasperDesign jasperDesign = null;
            JasperReport jasperReport = null;
    
            params.put("REPORT_DIR",yourClassName.class.getClassLoader()
                            .getResource("yourJasperFileName.jrxml").toString().replace("yourJasperFileName.jrxml", ""));
            jasperDesign = JasperManager.loadXmlDesign(yourClassName.class
                    .getClassLoader().getResourceAsStream("yourJasperFileName.jrxml"));
            params.put("joining_date", frm_dte);
             params.put("leaving_date", frm_dte);
            jasperReport = JasperCompileManager.compileReport(jasperDesign);
    
            /*
             * Filling the report with data from the database based on the
             * parameters passed.
             */
            myJPrint = JasperFillManager.fillReport(jasperReport, params, conn);
            params.clear();
    
        } catch (JRException ex) {
            ex.printStackTrace();
        }
    
        return myJPrint;
    
    }
    
    0 讨论(0)
  • 2020-12-20 10:08

    You can pass date as string format type as follow,

    if(from_date!=null)
    {
          formattedEndDate=new SimpleDateFormat("yyyy-MM-dd").format(from_date);
    }
    
    if(getStartDate()!=null)
    {
        formattedStartDate=new SimpleDateFormat("yyyy-MM-dd").format(to_date);
    }
    
    0 讨论(0)
提交回复
热议问题