在DynamicJasper中使用子报表

时间:2011-08-04 13:10:41

标签: grails jasper-reports subreport dynamic-jasper

我一直在Grails的一个项目上工作。我需要用pdf,excel和word生成报告。我也能够以上述格式生成报告。我使用DynamicJasper生成报告。

现在我需要在生成的报告中提交子报告。

生成的报告包含下表和饼图,现在我需要一个子报告,如下图所示,在同一个表中没有1  :

表1

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

报告期

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

生成报告的代码如下:

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

    switch (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
}

1 个答案:

答案 0 :(得分:0)

您可以从JasperReportBuilder创建一个类型对象,以设置要报告的属性:

JasperReportBuilder jrbSR = report();

使用SubreportBuilder创建一个Object:

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

打印子报告:

jrb.detail(subreportTotal);
相关问题