java jasper 生成xlsx,如何从jasper报告导出到excel中的多个工作表

I am working on a report with iReport that have many subreports.

I want to have two other sheets in the excel file when generating the report from my application.

When searching the internet I found answers about creating a break in the report, having the option "Ignore pagination " in the subreports "true" , but it is still not clear for me.

What are my options to control how and when a new sheet is created

解决方案

In jasper report there are different ways to achieve new sheet both in jrxml and in java code. The default behavior is to create a new sheet for every page. I will illustrated the 3 most common ways with relative problem in using them.

Ignore pagination and break element

Method

set isIgnorePagination="true" on the jasperReport tag and add

when you need a new sheet.

Problem: The report will not be beautiful if you export also to pdf (since its ignoring pagination)

Use the jrxml properties

Method

To avoid creating new sheet on every new page, set property

net.sf.jasperreports.export.xls.one.page.per.sheet="false"

And when you want it to create a new sheet before or after an reportElement add relative property:

net.sf.jasperreports.export.xls.break.before.row="true"

net.sf.jasperreports.export.xls.break.after.row="true"

Problem: The columns on every sheet will be the same and this can result in ugly colspan on different sheet's

Use java and controll the sheet's as you like (loading different reports)

Method

List sheets = new ArrayList();

for (int i=1;i<=8;i++){

JasperPrint print = JasperFillManager.fillReport("subReport_" + i + ".jasper", paramMap, connection);

sheets.add(print);

}

JRXlsxExporter exporter = new JRXlsxExporter();

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

exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(new File("text.xlxs"));

SimpleXlsxReportConfiguration configuration = new SimpleXlsxReportConfiguration();

configuration.setSheetNames(sheetNames): //sheets names is an array of the different names.

configuration.setOnePagePerSheet(false); //remove that it break on new page

configuration.setDetectCellType(true);

exporter.setConfiguration(configuration);

exporter.exportReport();

Problem: You can not use this method if you are using jasper report server.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值