如何将 JasperReport 导出到具有多个工作表的 Excel 文件?
我们有一份报告,客户希望将其导出为 Excel 格式,其中包含多个工作表。本质上,这两个查询共享相同的参数,但其他一切都不同。
在 jasper-reports 中,如何导出到具有多个工作表(最好来自不同数据源)的 Excel 文件?
We have a report that the customer would like to have exported to an excel format where it has multiple worksheets. Essentially the two queries share the same parameters, but everything else is different.
In jasper-reports how do you export to an excel file with multiple worksheets (ideally from different data sources)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
感谢这个线程,我确实更容易创建具有多个工作表的 Excel 导出。我发现您可以使用以下内容:
导出器将自动使用每个 JasperPrint 对象来构造每个工作表; Jasper 报告的名称(在 jrxml 文件中指定)也用作每个工作表的名称。
目前这个解决方案适用于我的本地项目,所以我只是想让你知道。
Thanks to this thread it really was easier for me to create an Excel export with multiple sheets. What I found out was that you could use the following:
and the exporter will automatically use every JasperPrint object to construct each sheet; also the name of the Jasper report (as specified in the jrxml file) is used as the name of each sheet.
Currently this solution works on my local project, so I just wanted to let you know.
感谢贝利萨留链接,我们似乎已经弄清楚了。基本操作方法是像平常一样为每个工作表创建 JasperPrint 对象。所以你有:
此时 JasperPrint 对象已经填充了数据源。然后你要做的:
它的作用是将
i
设置为firstWorkSheet
中当前的页数(应该是一个)。然后它循环遍历 SecondWorkSheet 中的页面并将它们添加到firstWorkSheet 中。确保在 jasperReport 中将每个工作表 jrxml 文件设置为打印为一页,这样就可以了。如果有任何变化我会更新,但这应该有效。
更新:
发现您需要使用
net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter
而不是
net.sf.jasperreports.engine.export.JRXlsExporter
因为导出到时似乎存在问题多个工作表。
此外,jrxml 文件中
isIgnorePagination
的设置需要为:isIgnorePagination="true"
,以便将每个 jrxml 文件导出为单个页面。
然后,您需要将 JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET 参数设置为 true,以便将每个页面分解为单独的工作表。
Thanks to belisarius link we seem to have figured it out. The basics of how to do it is create your JasperPrint objects for each sheet as you normally would. So you have:
The JasperPrint objects are already filled with the datasource at this point. Then you do:
What this does it sets
i
to the number of pages currently in thefirstWorkSheet
(which should be one). Then it loops thourgh the pages in thesecondWorkSheet
and adds them to the firstWorkSheet.Make sure in you jasperReport you have it set to print as one page for each of the work sheet jrxml files and you should be good to go. I will come update this if anything changes, but this should work.
UPDATE:
Discovered you need to use
net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter
instead of
net.sf.jasperreports.engine.export.JRXlsExporter
as there seems to be an issue when exporting to multiple work sheets.
Also the setting in the jrxml file for
isIgnorePagination
needs to be:isIgnorePagination="true"
so that each jrxml file is exported as a single page.
Then you need to set
JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET
parameter to true so it breaks out each page to a separate worksheet.根据当前版本 6.1.1,
JRXlsExporter.setParameter
已弃用。应将其替换为JRXlsExporter.setExporterInput
。因此,更新后的代码将是:As per current version 6.1.1,
JRXlsExporter.setParameter
is deprecated. It should be replaced byJRXlsExporter.setExporterInput
. So, the updated code would be: