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
    评论
JasperReports是一个流行的报生成框架,可以将报导出成多种格式,包括JPG图片。以下是Java代码示例,演示如何使用JasperReports将jasper文件导出成JPG图片: ```java import java.io.File; import java.io.FileOutputStream; import java.util.HashMap; import java.util.Map; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.util.JRLoader; public class JasperToJpgExample { public static void main(String[] args) { try { // 加载jasper文件 File jasperFile = new File("report.jasper"); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFile); // 填充数据 Map<String, Object> parameters = new HashMap<String, Object>(); // 设置报参数 parameters.put("param1", "value1"); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, new JREmptyDataSource()); // 导出为jpg图片 File jpgFile = new File("report.jpg"); FileOutputStream fos = new FileOutputStream(jpgFile); JasperExportManager.exportReportToPdfStream(jasperPrint, fos); System.out.println("Jasper导出为JPG图片。"); } catch (Exception e) { e.printStackTrace(); } } } ``` 需要注意的是,上述代码使用了JREmptyDataSource类作为数据源,这意味着报数据为空。如果需要填充数据,请使用适当的数据源。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值