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

发布于 09-19 17:26 字数 156 浏览 5 评论 0原文

我使用 iReport 3.7.4 版本制作了一份 jasper 报告,现在我必须使用它或在我使用 servlet、jsp 和 struts 框架、apache tomcat 作为服务器的 java 应用程序中调用该报告。

我想要有关如何通过一些示例调用 jasper 报告的步骤。

I made one jasper report using iReport 3.7.4 version, now i have to use that or call that report in my java application where i am using servlets, jsp and struts framework, apache tomcat as server.

I want steps regarding how to call the jasper report with some example.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

小红帽2024-09-26 17:26:02
  1. 在 iReport 中编译报告
  2. 将编译后的报告放在类路径中
  3. 使用

    加载它

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    
  4. dataSource 是您拥有的 DataSource 实例 - 例如 BeanCollectionDataSource

    JasperPrint jasperPrint = 
         JasperFillManager.fillReport(jasperReport, params, dataSource);
    
  5. 导出它

    JRPdfExporter 导出器 = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,outputStream);
    导出器.exportReport();
    
  6. 上面的 outputStream 可以是 response.getOutputStream()FileOutputStream() >,取决于您是要将其发送给客户端还是要将其存储为文件。如果您想将其发送到客户端,则必须发送 Content-Disposition 标头以及更多内容,但这取决于您要保存的格式。如果您想在客户端打印,这是一个完全不同的问题 - 您需要一些客户端代码,例如小程序。

  1. Compile the report in iReport
  2. Place the compiled report on the classpath
  3. load it with

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    
  4. Fill it with data. dataSource is the DataSource instance you have - for example a BeanCollectionDataSource

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

    JRPdfExporter exporter = new JRPdfExporter();
    exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
    exporter.exportReport();
    
  6. 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.

油饼2024-09-26 17:26:02

6年后@Bozho 答案现在(v5和v6)包含已弃用代码第5点
JRExporterParameter.OUTPUT_STREAM< /a>,但我会尽力改进其他方面


  1. 加载报告

    已编译版本.jasper

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    

    未编译版本.jrxml(较慢,因为需要编译但可行)

    JasperReport jasperReport = JasperCompileManager.compileReport("path/to/myReport.jrxml");
    
  2. 填写报告

    什么都没有(报告内生成的数据源或只是静态文本)

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

    数据源

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

    与数据库连接(可能最常见,在报表内执行sql)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, 连接);
    
  3. 导出报告

    JRPdfExporter 导出器 = new JRPdfExporter()
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
    SimplePdfExporterConfiguration 配置 = new SimplePdfExporterConfiguration();
    配置.setMetadataAuthor(“彼得”); //设置你的pdf配置, 
    导出器.setConfiguration(配置);
    导出器.exportReport();
    
  4. 如果您想将报告直接流式传输到网页,这就是contentTypeContent-disposition 设置以及如何检索 outputStream

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=myReport.pdf");
    OutputStream 输出流=response.getOutputStream();
    

After 6 years @Bozho answer now (v5 and v6) contains deprecated code on point 5
JRExporterParameter.OUTPUT_STREAM, but I will try to improve the other points while I'm at it

  1. Load the report

    compiled version .jasper

    JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream);
    

    or the non compiled version .jrxml (slower since need to compile but feasible)

    JasperReport jasperReport = JasperCompileManager.compileReport("path/to/myReport.jrxml");
    
  2. Fill the report

    with nothing (datasource generated inside report or just static text)

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

    with datasource:

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

    with database connection (may the most common, sql executed inside report)

    JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, connection);
    
  3. Export report

    JRPdfExporter exporter = new JRPdfExporter()
    exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
    SimplePdfExporterConfiguration configuration = new SimplePdfExporterConfiguration();
    configuration.setMetadataAuthor("Petter"); //Set your pdf configurations, 
    exporter.setConfiguration(configuration);
    exporter.exportReport();
    
  4. If you like to stream the report directly to web page this is how contentType and Content-disposition is set and how you retrieve the outputStream

    response.setContentType("application/x-pdf");
    response.setHeader("Content-disposition", "inline; filename=myReport.pdf");
    OutputStream outputStream = response.getOutputStream();
    
满意归宿2024-09-26 17:26:02

这段代码应该可以让您了解如何执行此操作

JasperReport jr=JasperCompileManager.compileReport("yourJRXMLFilePath");
JasperPrint jrPrint = JasperFillManager.fillReport(jr,mapWithParameters,aJRDataSource);
JasperExportManager.chooseYourFavoriteMethod(jrPrint,"destinationFile");

,否则,请检查 api
如果您已经使用 iReport 编译了该文件,则可以省略第一行。在这种情况下,请检查 JasperFillManager 上的 api 方法是否正确。

This piece of code should give you some idea on how to do it

JasperReport jr=JasperCompileManager.compileReport("yourJRXMLFilePath");
JasperPrint jrPrint = JasperFillManager.fillReport(jr,mapWithParameters,aJRDataSource);
JasperExportManager.chooseYourFavoriteMethod(jrPrint,"destinationFile");

Otherwise, check the api
The first line can be ommited if you had already compiled the file with iReport. Check the api for the correct method on JasperFillManager in this case.

奈何桥上唱咆哮2024-09-26 17:26:02

在第一个答案中,第5点:
之后

<代码>
JRPdfExporter 导出器= new JRPdfExporter();

添加

<代码>
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

in the first answer, point 5:
After


JRPdfExporter exporter= new JRPdfExporter();

add


exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);

伴梦长久2024-09-26 17:26:02

最佳解决方案(也为了更好的性能)将调用编译报告。

你可以看下面的例子

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromJasperFile {
  public static void main(String[] args) throws JRException, IOException {

    JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper",  new HashMap<String, Object>(), 
    new JREmptyDataSource());
    JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");

  }
}

Best solution (For better performance as well), will be calling a compiled report.

you can see the example below

import java.io.IOException;
import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;

public class PdfFromJasperFile {
  public static void main(String[] args) throws JRException, IOException {

    JasperPrint jasperPrint = JasperFillManager.fillReport("report.jasper",  new HashMap<String, Object>(), 
    new JREmptyDataSource());
    JasperExportManager.exportReportToPdfFile(jasperPrint, "sample.pdf");

  }
}
故事↓在人2024-09-26 17:26:02

这是做同样事情的不同方式。

    JasperReport jasperReport;
    JasperPrint jasperPrint;
    Map<String, Object> param = new HashMap<String, Object>();
    try{
        String sourceFileName = ".jrxml";
        jasperReport = JasperCompileManager.compileReport(sourceFileName);
        jasperPrint = JasperFillManager.fillReport(jasperReport,param,new JRBeanCollectionDataSource(getDetails()));
        JasperExportManager.exportReportToPdfFile(jasperPrint, ".pdf");
    }
    catch(Exception e){
    }

This is a different way of doing the same.

    JasperReport jasperReport;
    JasperPrint jasperPrint;
    Map<String, Object> param = new HashMap<String, Object>();
    try{
        String sourceFileName = ".jrxml";
        jasperReport = JasperCompileManager.compileReport(sourceFileName);
        jasperPrint = JasperFillManager.fillReport(jasperReport,param,new JRBeanCollectionDataSource(getDetails()));
        JasperExportManager.exportReportToPdfFile(jasperPrint, ".pdf");
    }
    catch(Exception e){
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文