如何在 jasper 报告中显示导出的 xml?

How to show exported xml in jasper report?

我们最近将 jasper 报告集成到我们的系统中。现在我们可以 运行 通过标准方式使用 jasper 报告向用户报告和显示结果,运行 并向用户显示结果。

但在某些情况下,我们需要将结果保存到对象存储中,并在请求时以文档形式显示给用户。据我所知 JasperPrint 是一个序列化对象。将报告结果作为序列化对象保存到对象存储不是一个好方法,因为我们体验过我们之前的报告工具。如果对象改变了序列化机制不能反序列化对象。

所以我们想将 jasper 结果以 xml 格式保存到对象存储,但我们找不到任何方法在 JRViewer 中显示导出的 xml。

有什么方法可以将导出的 xml 转换为可视形式吗?

Jasper Report 提供了两种保存和加载您填写的报告的方法,JasperPrint(注意:不包括导出为 pdf、xls ecc 等其他格式,因为加载和导出为其他格式非常困难).

感谢 @Robert Mugattarov, What is the difference between JasperReport formats?

.jrprint is a serialized JasperPrint object i.e. an actual report instance i.e. a template that has been filled with data. This file can be deserialized back into a JasperPrint object.

.jrpxml is a human readable XML represenatation of a JasperPrint object i.e. an XML version of a template that has been filled with data. This file can be unmarshalled back into a JasperPrint object.

既然你不希望有一个序列化的对象,剩下的解决方案就是中的jrpxml格式。

保存和加载到 jrpxml 的示例。

//Save JasperPrint to jrpxml (xml format)
JRXmlExporter exporter = new JRXmlExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleWriterExporterOutput(new File("myJasperPrint.jrpxml")));
exporter.exportReport();

//Load jrpxml to JasperPrint object
JasperPrint print = JRPrintXmlLoader.load("myJasperPrint.jrpxml");

//To show it in JasperViewer
JRViewer jrv = new JRViewer(print);

如果您需要减小文件大小,我建议您 zip/unzip jrpxml 文件。

What is a good Java library to zip/unzip files?