简体   繁体   English

JasperReport,展示和打印报告

[英]JasperReport, show and print report

I exported a .jrprint file created with iReport. 我导出了一个用iReport创建的.jrprint文件。 Now I want to preview the report and finally print it, how can I do this? 现在我想预览报告并最终打印出来,我该怎么做?

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

You're specifying the JasperPrint file and not the JasperReport file. 您正在指定JasperPrint文件而不是JasperReport文件。 Let me break down the files and what they are: 让我分解文件及其内容:

  • report.jrxml - An xml definition of a jasper report - this defines a report, but cannot be used directly to generate output. report.jrxml - jasper报告的xml定义 - 这定义了一个报告,但不能直接用于生成输出。
  • report.jasper - A compiled jrxml file (JasperReport). report.jasper - 已编译的jrxml文件(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.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。

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");
} 
}

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文件。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM