Java JasperReport、show和print报告

Java JasperReport、show和print报告,java,printing,jasper-reports,Java,Printing,Jasper Reports,我导出了使用iReport创建的.jrprint文件。 现在我想预览报告并最终打印它,我该怎么做 我正在尝试: JRBeanCollectionDataSource ds=new JRBeanCollectionDataSource(list); JasperPrint jrprint=JasperFillManager.fillReport("report.jrprint", null, ds); 但我有一个例外 java.lang.ClassCastException: net.sf.ja

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

我正在尝试:

JRBeanCollectionDataSource ds=new JRBeanCollectionDataSource(list);
JasperPrint jrprint=JasperFillManager.fillReport("report.jrprint", null, ds);
但我有一个例外

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

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

如果要获取PDF文件,可以使用以下来源:

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

jp是您的*.jrprint文件。

您指定的是JasperPrint文件,而不是JasperReport文件。让我将这些文件及其内容进行分解:

  • report.jrxml—jasper报表的xml定义—它定义报表,但不能直接用于生成输出
  • jasper—编译的jrxml文件(JasperReport)。这可以用作输入,用数据填充报告
  • 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");
连接连接=PersistenceSessionFactory.getSqlSession().getConnection(); JasperReport report=jaspecompilemanager.compileReport(“FancyPantsReport.jrxml”); //用于报告的设置参数 HashMap params=新的HashMap(); 参数put(“sqlDate”,fromDate); //从sql连接和参数填充报表数据 JasperPrint printedReport=JasperFillManager.fillReport(报告、参数、连接); String outputFilename=“FancyPants-”+dateString+“.pdf”; JasperExportManager.exportReportToPdfFile(printedReport,outputFilename); LOG.info(“在“+(System.currentTimeMillis()-start)+“ms”中生成的报告”);
请注意,它使用编译从jrxml获取JasperReport,然后使用FillManager从JasperReport获取JasperPrint,最后将JasperPrint导出为pdf。

您可以使用以下方法生成和打印报告:

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

您可以使用Jasper viewer预览报告并进行打印

这里有一个例子

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

嗯?从Jasper开始,这里没有足够的细节。