如何使用Export插件在Grails中生成多个报告? [英] How to generate multiple reports in Grails using Export plugin?

查看:113
本文介绍了如何使用Export插件在Grails中生成多个报告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Grails中的导出插件来生成PDF / Excel报表。我可以生成PDF / Excel按钮单击的单个报告。但是,现在我想单击按钮生成多个报告。我尝试了循环,方法调用,但没有运气。

I am using the Export plugin in Grails for generating PDF/Excel report. I am able to generate single report on PDF/Excel button click. But, now I want to generate multiple reports on single button click. I tried for loop, method calls but no luck.

引用链接没问题。我不期望整个代码,只需要参考。

Reference links are ok. I don't expect entire code, needs reference only.

推荐答案

如果您看一下 ExportService 插件你会发现有各种各样的 export 方法。其中两个支持 OutputStream s。使用这两种方法(取决于您对其他参数的要求)将允许您将报告呈现给输出流。然后使用这些输出流,您可以创建一个zip文件,您可以将它传递给HTTP客户端。

If you take a look at the source code for the ExportService in the plugin you will notice there are various export methods. Two of which support OutputStreams. Using either of these methods (depending on your requirements for the other parameters) will allow you to render a report to an output stream. Then using those output streams you can create a zip file which you can deliver to the HTTP client.

这是一个非常粗略的例子,它写在我的顶部所以它实际上只是一个想法而不是工作代码:

Here is a very rough example, which was written off the top of my head so it's really just an idea rather than working code:

// Assumes you have a list of maps.
// Each map will have two keys.
// outputStream and fileName
List files = []

// call the exportService and populate your list of files
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
// exportService.export('pdf', outputStream, ...)
// files.add([outputStream: outputStream, fileName: 'whatever.pdf'])

// ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream()
// exportService.export('pdf', outputStream2, ...)
// files.add([outputStream: outputStream2, fileName: 'another.pdf'])

// create a tempoary file for the zip file
File tempZipFile = File.createTempFile("temp", "zip")
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tempZipFile))

// set the compression ratio
out.setLevel(Deflater.BEST_SPEED);

// Iterate through the list of files adding them to the ZIP file
files.each { file ->
    // Associate an input stream for the current file
    ByteArrayInputStream input = new ByteArrayInputStream(file.outputStream.toByteArray())

    // Add ZIP entry to output stream.
    out.putNextEntry(new ZipEntry(file.fileName))

    // Transfer bytes from the current file to the ZIP file
    org.apache.commons.io.IOUtils.copy(input, out);

    // Close the current entry
    out.closeEntry()

    // Close the current input stream
    input.close()
}

// close the ZIP file
out.close()

// next you need to deliver the zip file to the HTTP client
response.setContentType("application/zip")
response.setHeader("Content-disposition", "attachment;filename=WhateverFilename.zip")
org.apache.commons.io.IOUtils.copy((new FileInputStream(tempZipFile), response.outputStream)
response.outputStream.flush()
response.outputStream.close()

这应该给你一个如何解决这个问题的想法。同样,以上仅仅是为了演示的目的,并不是生产就绪代码,我甚至没有试图编译它。

That should give you an idea of how to approach this. Again, the above is just for demonstration purposes and isn't production ready code, nor have I even attempted to compile it.

这篇关于如何使用Export插件在Grails中生成多个报告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆