开发者

Using subreport in DynamicJasper

I have been working on a project in Grails. I need to generate reports in pdf, excel, and word. I have been able to generate reports also in the above mentioned formats. I have used DynamicJasper to generate the reports.

Now I need sub reports in the generated report.

The generated reports contain the following table along with a pie chart, and now I need a sub report like shown below in this same table no 1 :

Table no 1

SN |    Top 20          |Reporting     |Comparison  
1  |    Dorsopathies    |$181           |$5     
2  |    Special         |$23            |$6     
3  |    Other Wounds    |$20            |$0 

Reporting Period

Members| M Cost         |P Cost     
  47   | $4,995,541     |$1,598,049     
234    | $2,576,497     |$824,209

My code to generate the report is like this:

if (params.format) {
        String svg = params.svg
        def imagePath = fetchService.getChartImage(svg)

        FastReportBuilder drb = new FastReportBuilder();
        if (imagePath && imagePath!='') {
            drb.setTemplateFile(root+"/reports/template_chart.jrxml")
        } else {
            drb.setTemplateFile(root+"/reports/template.jrxml")
        }
        Style changeStyle = new Style()
        changeStyle.setHorizontalAlign(HorizontalAlign.CENTER)

        DynamicReport dr = drb.addColumn("S.N", "S.N",Integer.class.getName(),30, changeStyle).
                addColumn("Top 20", "Top 20 ",String.class.getName(),150).
                addColumn("Reporting ","Reporting ",Double.class.getName(), 50, false, '$0.00').
                addColumn("Comparison ","Comparison ",Double.class.getName(), 50, false, '$0.00').

                addColumn("Prior Period Ranking","Prior Period Ranking",String.class.getName(), 50, changeStyle).
                setTitle(" ").

                setUseFullPageWidth(true).

                build();

        JRDataSource ds = new JRBeanCollectionDataSource(data);
        JasperPrint jp = DynamicJasperHelper.generateJasperPrint(dr,new ClassicLayoutManager(), ds);


        HashMap reportParam = new HashMap();
        reportParam.put("TITLE","Top 20 ");
        reportParam.put("IMAGE_LINK",root+"/images/report_logo.jpg");
        reportParam.put("CHART_IMAGE",imagePath);

        generateReport(params.format, jp)

        fetchService.deleteTempFile(imagePath)
        return
    }


    [data: data, columns: columns , totalRep : totalRep, totalCom:totalCom, currentPage:'']
}


def generateReport(String format, JasperPrint jp){
    ReportWriter reportWriter

    开发者_如何学JAVAswitch (format) {
        case "word":
            JRDocxExporter docExporter = new JRDocxExporter();
            ByteArrayOutputStream docStream = new ByteArrayOutputStream();
            docExporter.setParameter(JRDocxExporterParameter.JASPER_PRINT,jp);
            docExporter.setParameter(JRDocxExporterParameter.OUTPUT_STREAM,docStream);
            docExporter.setParameter(JRDocxExporterParameter.FLEXIBLE_ROW_HEIGHT, Boolean.TRUE);
            docExporter.exportReport();
            def docResume = docStream.toByteArray();

            response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".docx\"");
            response.outputStream << docResume

        case "excel":
            reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, 'XLS', [:]);
            response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".xls\"");
            break

        default:
            reportWriter = ReportWriterFactory.getInstance().getReportWriter(jp, 'PDF', [:]);
            response.setHeader("Content-disposition", "attachment; filename=\"" + "Report" + ".pdf\"");

            break
    }
    reportWriter?.writeTo(response);

    return
}


You can create a type object from JasperReportBuilder to set Properties to report:

JasperReportBuilder jrbSR = report();

With SubreportBuilder create an Object:

SubreportBuilder Your_OBject= cmp.subreport(your_method(columns, your_data));

Print your subreport:

jrb.detail(subreportTotal);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜