Getting Started With JasperReports

来源:百度文库 编辑:神马文学网 时间:2024/04/29 12:42:14
David R. Heffelfinger Introduction
I‘ve recently been researching reporting tools for a project I will be soon be working on. One of the tools I‘ve been looking at is JasperReports. JasperReports is a very popular open source (LGPL) reporting library written in Java. Unfortunately it is not very well documented and I had a hard time coming up with a simple report. After some research, I was able to generate a simple report, this article summarizes what needs to be done to get started with JasperReports. See Resources for information on how to find additional information and documentation about JasperReports.
Getting Started
JasperReports‘ reports are defined in XML files, which by convention have an extension of jrxml. A typical jrxml file contains the following elements:
- the root element. - its contents are printed only once at the beginning of the report <pageHeader> - its contents are printed at the beginning of every page in the report. <detail> - contains the body of the report. <pageFooter> - its contents are printed at the bottom of every page in the report. <band> - defines a report section, all of the above elements contain a band element as its only child element.<br>All of the elements are optional, except for the root jasperReport element. Here is an example jrxml file that will generate a simple report displaying the string "Hello World!"<br><?xml version="2.0"?><br><!DOCTYPE jasperReport<br>PUBLIC "-//JasperReports//DTD Report Design//EN"<br>"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd"><br><jasperReport name="Simple_Report"><br><detail><br><band height="20"><br><staticText><br><reportElement x="180" y="0" width="200" height="20"/><br><text><![CDATA[Hello World!]]></text><br></staticText><br></band><br></detail><br></jasperReport><br>For this simple example, I omitted the optional <title>, <pageHeader> and <pageFooter> elements. The <staticText> element, unsurprisingly, displays static text on the report, as can be seen, it contains a single <text> element defining the text that will be displayed.<br>jrxml files need to be "compiled" into a binary format that is specific to JasperReports, this can be achieved by calling the compileReport() method on the net.sf.jasperreports.engine.JasperCompileManager class. There are several overloaded versions of this method, in our example, we will use the one that takes a single String parameter, consult the JasperReport documentation for details on the other versions of the method.<br>public class JasperReportsIntro{ public static void main(String[] args) { JasperReport jasperReport; JasperPrint jasperPrint; try { jasperReport = JasperCompileManager.compileReport(<br>"reports/jasperreports_demo.jrxml");<br>jasperPrint = JasperFillManager.fillReport(<br>jasperReport, new HashMap(), new JREmptyDataSource());<br>JasperExportManager.exportReportToPdfFile(<br>jasperPrint, "reports/simple_report.pdf"); } catch (JRException e) { e.printStackTrace(); } }}<br>A jrxml file needs to be compiled only once, but for this simple example it is compiled every time the application is executed. Before a report can be generated, it needs to be "filled" with data, this is achieved by calling the fillReport() method on the net.sf.jasperreports.engine.JasperFillManager class. Again, there are several overloaded versions of the fillReport() method, here we will use one that takes three parameters, an instance of net.sf.jasperreports.engine.JasperReport, a java.util.HashMap containing any parameters passed to the report, and an instance of a class implementing the net.sf.jasperreports.engine.JRDataSource interface. The line that accomplishes this in our example above is<br>jasperPrint = JasperFillManager.fillReport( jasperReport, new HashMap(), new JREmptyDataSource());Since our simple report takes no parameters, we pass an empty HashMap as the second parameter, and an instance of net.sf.jasperreports.engine.JREmptyDataSource as the third parameter. JREmptyDataSource is a convenience class included with JasperReports, it is basically a DataSource with no data.<br>Finally, in our example, we export the report to a PDF file, this way it can be read by Adobe Acrobat, XPDF, Evince, or any other PDF reader, the line that accomplishes this in our example is<br>JasperExportManager.exportReportToPdfFile(<br>jasperPrint, "reports/simple_report.pdf");The parameters are self-explanatory. Conclusion<br>JasperReports is a very good and popular open source reporting engine, this guide provides enough information to get started with JasperReports. For additional documentation, JasperSoft publishes an eBook titledThe JasperReports Ultimate Guide . Resources<br>Displaying JasperReports PDF Reports on the BrowserJasperReports‘ Web SiteThe JasperReports Ultimate Guide - an eBook published by JasperSoft, creators of JasperReports<br>Comments?Contact the author.</div> <div class="list-group"> <a href="/article/4530" class="list-group-item">Getting Started With JasperReports</a> <a href="/article/5459" class="list-group-item">Getting Started with Eclipse and the SWT - hunterK的专栏</a> <a href="/article/58210" class="list-group-item">Getting started with the Oracle BPEL Process Manager</a> <a href="/article/127490" class="list-group-item">The Process (or undertaking) of Getting Started with OFBiz</a> <a href="/article/928425" class="list-group-item">Part 1 - Getting Started</a> <a href="/article/2680128" class="list-group-item">VisualSVN Server | Getting started</a> <a href="/article/115966" class="list-group-item">Google Gadgets API - Getting Started</a> <a href="/article/2804611" class="list-group-item">Tutorial: Getting Started Using RMI-IIOP</a> <a href="/article/2395326" class="list-group-item">2010/10-29 Getting Up With You</a> <a href="/article/104854" class="list-group-item">Getting Your Feet Wet with the SWT StyledText Widget</a> <a href="/article/327389" class="list-group-item">Geek To Live: Getting Things Done with Google Notebook</a> <a href="/article/196461" class="list-group-item">使用JasperReports</a> <a href="/article/905426" class="list-group-item">使用JasperReports</a> <a href="/article/1091811" class="list-group-item">Getting to Grips with Latex - Tables - Latex Tutorials by Andrew Roberts @ School of Computing, University of Leeds</a> <a href="/article/106724" class="list-group-item">ireport jasperreports报表</a> <a href="/article/597389" class="list-group-item">Getting intense</a> <a href="/article/2114968" class="list-group-item">It Started Online</a> <a href="/article/821244" class="list-group-item">Getting Old Before Getting Rich</a> <a href="/article/196457" class="list-group-item">对JasperReports的调研小结</a> <a href="/article/1645811" class="list-group-item">Getting Things Done: Getting Projects Under Control</a> <a href="/article/5217" class="list-group-item">Getting to Know Groovy</a> <a href="/article/148869" class="list-group-item">再看Getting Real</a> <a href="/article/183330" class="list-group-item">Getting Real: Code Speaks</a> <a href="/article/2875660" class="list-group-item">always getting over you</a> </div> </div> </div> </div> </div> </div> <footer id="footer" class="footer hidden-print"> <div class="container"> <div class="panel panel-default"> <div class="panel-heading">相关问题</div> <div class="panel-body"> <a class="btn btn-default" href="/article/1804054" title="2010qq">2010qq</a> <a class="btn btn-default" href="/article/1804055" title="岁月,苍老人生">岁月,苍老人生</a> <a class="btn btn-default" href="/article/1804056" title="八月,没有故事">八月,没有故事</a> <a class="btn btn-default" href="/article/1804057" title="纪念抗战胜利65周年座谈会">纪念抗战胜利65周年座谈会</a> <a class="btn btn-default" href="/article/1804058" title="童言爆笑语录,笑了就要顶哦!!!">童言爆笑语录,笑了就要顶哦!!!</a> <a class="btn btn-default" href="/article/1804059" title="酒是女人最好的情人">酒是女人最好的情人</a> <a class="btn btn-default" href="/article/1804060" title="几米漫画之-人生不过如此">几米漫画之-人生不过如此</a> <a class="btn btn-default" href="/article/1804061" title="男人提升“威力”的十种方法353553">男人提升“威力”的十种方法353553</a> <a class="btn btn-default" href="/article/1804062" title="养生妙方——自劝词">养生妙方——自劝词</a> <a class="btn btn-default" href="/article/1804063" title="别迷恋“捡漏”老太 那只是传说">别迷恋“捡漏”老太 那只是传说</a> <a class="btn btn-default" href="/article/1804064" title="机关生存法则323232">机关生存法则323232</a> <a class="btn btn-default" href="/article/1804065" title="糖醋里脊">糖醋里脊</a> <a class="btn btn-default" href="/article/1804066" title="意男子秘建地下神庙 堪称世界第八奇迹(图)">意男子秘建地下神庙 堪称世界第八奇迹(图)</a> <a class="btn btn-default" href="/article/1804067" title="102 十二时辰养生要点">102 十二时辰养生要点</a> <a class="btn btn-default" href="/article/1804068" title="微生物细胞的结构和功能">微生物细胞的结构和功能</a> <a class="btn btn-default" href="/article/1804069" title="一个大学生从月薪3500到700万和他的情感经历">一个大学生从月薪3500到700万和他的情感经历</a> <a class="btn btn-default" href="/article/1804070" title="18岁舟曲男孩徒手掰断钢筋逃生 救己不忘助人">18岁舟曲男孩徒手掰断钢筋逃生 救己不忘助人</a> <a class="btn btn-default" href="/article/1804071" title="哪些皮肤病会“秋后算账”?_">哪些皮肤病会“秋后算账”?_</a> <a class="btn btn-default" href="/article/1804072" title="个人简历中5类常用英语词汇完全汇总(转载)">个人简历中5类常用英语词汇完全汇总(转载)</a> <a class="btn btn-default" href="/article/1804073" title="李商隐:此情可待成追忆">李商隐:此情可待成追忆</a> <a class="btn btn-default" href="/article/1804074" title="殿堂级的蓝纹绵羊奶奶酪">殿堂级的蓝纹绵羊奶奶酪</a> <a class="btn btn-default" href="/article/1804075" title="女人变坏男人多半不知不觉2">女人变坏男人多半不知不觉2</a> <a class="btn btn-default" href="/article/1804076" title="向我们敬爱的周总理致敬">向我们敬爱的周总理致敬</a> <a class="btn btn-default" href="/article/1804077" title="103 五脏六腑指的是什么">103 五脏六腑指的是什么</a> <a class="btn btn-default" href="/article/1804078" title="一生必看的成功学书h">一生必看的成功学书h</a> <a class="btn btn-default" href="/article/1804079" title="10条幸福的秘诀">10条幸福的秘诀</a> <a class="btn btn-default" href="/article/1804080" title="揭秘:一八O师朝鲜战场受重创内情">揭秘:一八O师朝鲜战场受重创内情</a> <a class="btn btn-default" href="/article/1804081" title="用Word2010让汉字拼音随心安放">用Word2010让汉字拼音随心安放</a> <a class="btn btn-default" href="/article/1804082" title="心理养生的四要诀">心理养生的四要诀</a> <a class="btn btn-default" href="/article/1804083" title="我喜欢这些话,很激励人。">我喜欢这些话,很激励人。</a> <a class="btn btn-default" href="/article/1804084" title="道教养生学的形成与发展简述2">道教养生学的形成与发展简述2</a> </div> </div></div> <div class="copy-right"> <p>神马文学网,客观、专业、权威的知识性互动百科全书。</p></div> </footer> </body> </html>