在 DynamicJasper 中使用子报表

发布于 2024-11-27 22:10:39 字数 3559 浏览 2 评论 0原文

我一直在用 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
}

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

    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
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那些过往 2024-12-04 22:10:39

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

JasperReportBuilder jrbSR = report();

使用 SubreportBuilder 创建一个对象:

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

打印您的子报表:

jrb.detail(subreportTotal);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文