且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JasperReports:如何在jsp页面中调用报表

更新时间:2023-10-06 12:41:58

  1. 在 iReport 中编译报告
  2. 将编译后的报告放在类路径上
JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);

  • 用数据填充它.dataSource 是您拥有的 DataSource 实例 - 例如一个 BeanCollectionDataSource

  • Fill it with data. dataSource is the DataSource instance you have - for example a BeanCollectionDataSource

    JasperPrint jasperPrint = 
         JasperFillManager.fillReport(jasperReport, params, dataSource);
    

  • 导出

  • Export it

    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
    exporter.exportReport();
    

  • 上面的 outputStream 可以是 response.getOutputStream()FileOutputStream(),这取决于你是否想要将其发送给客户端,或者您想将其存储为文件.如果要将其发送给客户端,则必须发送 Content-Disposition 标头,以及其他一些标头,但这取决于要保存的格式.如果您想在客户端打印,这是一个完全不同的问题 - 您需要一些客户端代码,例如一个小程序.

  • The outputStream above may be either a response.getOutputStream() or a FileOutputStream(), depending on whether you want to send it to a client or you want to store it as a file. If you want to send it to the client, you'd have to send the Content-Disposition header, and some more, but that depends on the format you want to save to. In case you want to print on the client, it's quite a different question - you'd need some client-side code, an applet, for example.