ireport是工具,jasper才是核心。没有ireport也可以做报表(熟悉jasper报表xml格式即可)。
很自然地,那么多的资料,哪里的最好?当然是jasper开源的例子最好!
有关资源可以到http://jasper.sourceforge.net/下载
https://sourceforge.net/projects/jasper/ 提供比较可靠的下载
https://jasper.svn.sourceforge.net/svnroot/jasper/ 提供比较新的下载。
目前他们提供的svn帐号是anonsvn/anonsvn
有许多的例子,看了之后,就会豁然开朗。
因为有太多的例子,现在有50个左右,逐一介绍是没有必要的。
其中有四个是比较值得看的:
1)datasource, 学习定制自己的JRdatasource
2) 一个是jasper,可以了解如何操纵jasper文件
3) 一个是webapp,如何实现网络打印jrprint文件或者数据流。
4) CHARTS,有关于所有chart的示例。
这其中,大部分的代码是类似的。
通过java开发工具netbeans或者eclipse或者jbuilder可以查看 jasperreports-3.0.0.jar,jasperreports-3.0.0-applet.jar所包含的类的内容,具体需要什么版本,可以到相关网站下载。
最后,把比较有代表性的抄录如下:
public static void main(String[] args) { if(args.length == 0) { usage(); return; } String taskName = args[0]; String fileName = args[1]; try { long start = System.currentTimeMillis(); if (TASK_FILL.equals(taskName)) { JasperFillManager.fillReportToFile(fileName, null, new JREmptyDataSource()); System.err.println("Filling time : " + (System.currentTimeMillis() - start)); } else if (TASK_PRINT.equals(taskName)) { JasperPrintManager.printReport(fileName, true); System.err.println("Printing time : " + (System.currentTimeMillis() - start)); } else if (TASK_PDF.equals(taskName)) { File sourceFile = new File(fileName); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".pdf"); JRPdfExporter exporter = new JRPdfExporter(); HashMap fontMap = new HashMap(); FontKey key = new FontKey("DejaVu Serif", true, false); PdfFont font = new PdfFont("DejaVuSerif-Bold.ttf", "Cp1252", true); fontMap.put(key, font); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap); exporter.exportReport(); System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_XML.equals(taskName)) { JasperExportManager.exportReportToXmlFile(fileName, false); System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_XML_EMBED.equals(taskName)) { JasperExportManager.exportReportToXmlFile(fileName, true); System.err.println("XML creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_HTML.equals(taskName)) { File sourceFile = new File(fileName); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".html"); JRHtmlExporter exporter = new JRHtmlExporter(); HashMap fontMap = new HashMap(); fontMap.put("sansserif", "DejaVu Sans, Arial, Verdana"); fontMap.put("serif", "DejaVu Serif, Times New Roman"); fontMap.put("monospaced", "DejaVu Sans Mono, Courier, Courier New"); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap); exporter.exportReport(); System.err.println("HTML creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_RTF.equals(taskName)) { File sourceFile = new File(fileName); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".rtf"); HashMap fontMap = new HashMap(); fontMap.put("sansserif", "DejaVu Sans"); fontMap.put("serif", "DejaVu Serif"); fontMap.put("monospaced", "DejaVu Sans Mono"); JRRtfExporter exporter = new JRRtfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap); exporter.exportReport(); System.err.println("RTF creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_XLS.equals(taskName)) { File sourceFile = new File(fileName); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".xls"); JRXlsExporter exporter = new JRXlsExporter(); HashMap fontMap = new HashMap(); fontMap.put("sansserif", "DejaVu Sans"); fontMap.put("serif", "DejaVu Serif"); fontMap.put("monospaced", "DejaVu Sans Mono"); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap); exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_JXL.equals(taskName)) { File sourceFile = new File(fileName); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".jxl.xls"); JExcelApiExporter exporter = new JExcelApiExporter(); HashMap fontMap = new HashMap(); fontMap.put("sansserif", "DejaVu Sans"); fontMap.put("serif", "DejaVu Serif"); fontMap.put("monospaced", "DejaVu Sans Mono"); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); exporter.setParameter(JRExporterParameter.FONT_MAP, fontMap); exporter.exportReport(); System.err.println("XLS creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_CSV.equals(taskName)) { File sourceFile = new File(fileName); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".csv"); JRCsvExporter exporter = new JRCsvExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.exportReport(); System.err.println("CSV creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_ODT.equals(taskName)) { File sourceFile = new File(fileName); JasperPrint jasperPrint = (JasperPrint)JRLoader.loadObject(sourceFile); File destFile = new File(sourceFile.getParent(), jasperPrint.getName() + ".odt"); JROdtExporter exporter = new JROdtExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); exporter.exportReport(); System.err.println("ODT creation time : " + (System.currentTimeMillis() - start)); } else if (TASK_RUN.equals(taskName)) { JasperRunManager.runReportToPdfFile(fileName, null, new JREmptyDataSource()); System.err.println("PDF running time : " + (System.currentTimeMillis() - start)); } else { usage(); } } catch (JRException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }