Java JasperReport,显示和打印报告

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4265401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:09:55  来源:igfitidea点击:

JasperReport, show and print report

javaprintingjasper-reports

提问by blow

I exported a .jrprint file created with iReport. Now I want to preview the report and finally print it, how can I do this?

我导出了一个用 iReport 创建的 .jrprint 文件。现在我想预览报告并最终打印它,我该怎么做?

I'm trying with:

我正在尝试:

JRBeanCollectionDataSource ds=new JRBeanCollectionDataSource(list);
JasperPrint jrprint=JasperFillManager.fillReport("report.jrprint", null, ds);

But I have this exception

但我有这个例外

java.lang.ClassCastException: net.sf.jasperreports.engine.JasperPrint cannot be cast to net.sf.jasperreports.engine.JasperReport

回答by Ziagl

if you want to print a JasperReport you have to call the fillReport with a JasperReport file (*.jasper).

如果要打印 JasperReport,则必须使用 JasperReport 文件 (*.jasper) 调用 fillReport。

If you want to get an PDF file you may use following source:

如果您想获得 PDF 文件,您可以使用以下来源:

JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jp);
exporter.setParameter(JRExporterParameter.OUTPUT_FILE, outFile);
exporter.exportReport();

jp is here your *.jrprint file.

jp 在这里是您的 *.jrprint 文件。

回答by Kieveli

You're specifying the JasperPrint file and not the JasperReport file. Let me break down the files and what they are:

您指定的是 JasperPrint 文件而不是 JasperReport 文件。让我分解文件和它们是什么:

  • report.jrxml - An xml definition of a jasper report - this defines a report, but cannot be used directly to generate output.
  • report.jasper - A compiled jrxml file (JasperReport). This can be used as input to fill the report with data.
  • report.jprint - A report that's been filled with data, and is ready to be exported to multiple output formats
  • report.jrxml - jasper 报告的 xml 定义 - 这定义了一个报告,但不能直接用于生成输出。
  • report.jasper - 编译后的 jrxml 文件 (JasperReport)。这可以用作用数据填充报告的输入。
  • report.jprint - 已填充数据并准备导出为多种输出格式的报告

Here's some code to start with the jrxml file the designer creates to get you to an printed pdf output:

下面是一些代码,从设计者创建的 jrxml 文件开始,让您获得打印的 pdf 输出:

Connection connection = PersistenceSessionFactory.getSqlSession().getConnection();
JasperReport report = JasperCompileManager.compileReport( "FancyPantsReport.jrxml" );

// setup parameters for use with the report
HashMap<String, Object> params = new HashMap<String,Object>();
params.put( "sqlDate", fromDate );

// Fill the report data from the sql connection and parameters
JasperPrint printedReport = JasperFillManager.fillReport(report, params, connection);

String outputFilename = "FancyPants-" + dateString + ".pdf";
JasperExportManager.exportReportToPdfFile( printedReport, outputFilename );

LOG.info("Report Generated in " + (System.currentTimeMillis() - start) + "ms");

Notice it uses the compile to get a JasperReport from the jrxml, then the FillManager to get a JasperPrint from the JasperReport, and finally exports the JasperPrint to pdf.

注意它使用 compile 从 jrxml 中获取 JasperReport,然后使用 FillManager 从 JasperReport 中获取 JasperPrint,最后将 JasperPrint 导出为 pdf。

回答by user3249098

You can use the following to produce and print the report:

您可以使用以下内容来生成和打印报告:

JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(list);
InputStream jasperStream = YourClass.class.getResourceAsStream(TEMPLATE_BASE_PATH);
 JasperPrint jasperPrint = JasperFillManager.fillReport(jasperStream, parameters, dataSource);
JasperViewer viewer = new JasperViewer(jasperPrint, false);
viewer.setVisible(true);

回答by chalitha geekiyanage

You can use Jasper viewer to preview reports and print it.

您可以使用 Jasper 查看器来预览和打印报告。

Here is an example!

这是一个例子!

public void generateReport() throws PrinterException {

try {  
String sourceFileName = "src/bill/report.jasper";
String printFileName = null;
Purchase_BeanFactory DataBean = new Purchase_BeanFactory();
JRBeanCollectionDataSource beanColDataSource = new     JRBeanCollectionDataSource(DataBean.generateCollection());
Map parameters = new HashMap();
printFileName = JasperFillManager.fillReportToFile(
     sourceFileName,
     parameters,
     beanColDataSource);

JasperViewer jv=new JasperViewer("src/bill/report.jrprint", false, false);

//set title for the jasper viewer
jv.setTitle("Your Title");

jv.setVisible(true);
//set icon to the jasper viewer
jv.setIconImage(
(new 
ImageIcon(getClass().getResource("path/to/image.png")).getImage())); 

} catch (Exception e) {
System.out.println("e");
} 
}