如何将 JasperReport 导出到具有多个工作表的 Excel 文件?

发布于 2024-09-28 10:36:48 字数 150 浏览 4 评论 0原文

我们有一份报告,客户希望将其导出为 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 技术交流群。

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

发布评论

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

评论(3

冷了相思 2024-10-05 10:36:48

感谢这个线程,我确实更容易创建具有多个工作表的 Excel 导出。我发现您可以使用以下内容:

ArrayList<JasperPrint> list = new  ArrayList<JasperPrint>();
list.add(jp1); list.add(jp2);
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT_LIST, list);

导出器将自动使用每个 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:

ArrayList<JasperPrint> list = new  ArrayList<JasperPrint>();
list.add(jp1); list.add(jp2);
exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT_LIST, list);

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.

浮萍、无处依 2024-10-05 10:36:48

感谢贝利萨留链接,我们似乎已经弄清楚了。基本操作方法是像平常一样为每个工作表创建 JasperPrint 对象。所以你有:

JasperPrint firstWorkSheet = ...;
JasperPrint secondWorkSheet = ...;

此时 JasperPrint 对象已经填充了数据源。然后你要做的:

List<JRPrintPage> pages = new ArrayList<JRPrintPage>(secondWorkSheet.getPages());
int i = firstWorkSheet.getPages().size();
for (int count = 0; count < pages.size(); count++) {
    firstWorkSheet.addPage(i, (JRPrintPage) pages.get(count));
    i++;
}

它的作用是将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:

JasperPrint firstWorkSheet = ...;
JasperPrint secondWorkSheet = ...;

The JasperPrint objects are already filled with the datasource at this point. Then you do:

List<JRPrintPage> pages = new ArrayList<JRPrintPage>(secondWorkSheet.getPages());
int i = firstWorkSheet.getPages().size();
for (int count = 0; count < pages.size(); count++) {
    firstWorkSheet.addPage(i, (JRPrintPage) pages.get(count));
    i++;
}

What this does it sets i to the number of pages currently in the firstWorkSheet (which should be one). Then it loops thourgh the pages in the secondWorkSheet 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.

愁以何悠 2024-10-05 10:36:48

根据当前版本 6.1.1,JRXlsExporter.setParameter 已弃用。应将其替换为 JRXlsExporter.setExporterInput。因此,更新后的代码将是:

ArrayList<JasperPrint> sheets = new ArrayList<JasperPrint>();
sheets.add(sheet1); 
sheets.add(sheet2);

exporter.setExporterInput(SimpleExporterInput.getInstance(sheets));

As per current version 6.1.1, JRXlsExporter.setParameter is deprecated. It should be replaced by JRXlsExporter.setExporterInput. So, the updated code would be:

ArrayList<JasperPrint> sheets = new ArrayList<JasperPrint>();
sheets.add(sheet1); 
sheets.add(sheet2);

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