我导出了一个用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
答案 0 :(得分:6)
您指定的是JasperPrint文件,而不是JasperReport文件。让我分解文件及其内容:
以下是一些代码,它们从设计人员创建的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");
注意它使用compile从jrxml获取JasperReport,然后使用FillManager从JasperReport获取JasperPrint,最后将JasperPrint导出为pdf。
答案 1 :(得分:3)
您可以使用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");
}
}
答案 2 :(得分:2)
如果要打印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文件。
答案 3 :(得分:0)
您可以使用以下内容制作和打印报告:
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);