JasperReport report, show and print

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

I'm trying to:

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
+3
source share
4 answers

You specify the JasperPrint file, not the JasperReport file. Let me split the files and what they are:

  • report.jrxml - jasper report definition in xml - this defines the report, but cannot be used directly to generate output.
  • report.jasper - compiled jrxml file (JasperReport). This can be used as an input to populate the report with data.
  • report.jprint - , , .

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

, JasperReport jrxml, FillManager, JasperPrint JasperReport , , JasperPrint pdf.

+6

Jasper . ​​

!

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

JasperReport, fillReport JasperReport (*.jasper).

PDF , :

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

jp - *.jrprint.

+2

:

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

Source: https://habr.com/ru/post/1776657/


All Articles