Javaweb+jasperreport Report Development Example

Source: Internet
Author: User
Tags cdata

Search on the Internet a lot of examples of Jasperreport report development, basically are said some of the theoretical knowledge, not a direct use of code to express, presumably everyone want to learn the beginning of the Jasperreport report, all want to be able to see the code visually, study the code after the implementation of the effect is what, Rather than a whole bunch of theories, it's better to just look at the code and write more notes in the code than it is for the beginning of the study.

This article uses the latest version of Jasperreport 6.0

Jasperreport Official website: community.jaspersoft.com

Web Development Recommendations Download JasperReports Library---Jasperreports-xxx-project with demo and API


Project structure:

Jar Package: The Novice can put all the downloaded jar packs in, proficiency can be in accordance with their own needs to join the relevant jar package

Webroot: Create a new reports folder for storing Jrxml, jasper files, build.xml (the ant icon will appear after putting in);



Jrxml file code, this code is an XML format file, you can write your own handwriting, you can also install ireport design compiled into Jasper files, in order to get started under the pure handwriting

Dbreport.jrxml:

<?xml version= "1.0" encoding= "UTF-8"? ><jasperreport xmlns= "http://jasperreports.sourceforge.net/ JasperReports "xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance "xsi:schemalocation="/HTTP/ Jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd "Name=" Dbreport "><style name=" PageHeader "forecolor=" #FFFFFF "backcolor=" #333333 "/><parameter name=" UserName " class= "java.lang.String"/><!--$P {} is a fill parameter, $V {} is a value, $F {} is a query value--><querystring><! [Cdata[select Id,user_name as name,email,qq from user where user_name= $P {username}]]></querystring> <field Name= "id" class= "java.lang.Integer"/><field name= "name" class= "java.lang.String"/><field name= "Email" class= "java.lang.String"/><field name= "QQ" class= "java.lang.String"/><pageheader><band height= " "><statictext><reportelement style=" PageHeader "x=" 0 "y=" 5 "width=" on "height="/>< " TextElement textalignment= "CEnter "/><text><! [Cdata[id]]></text></statictext><statictext><reportelement style= "PageHeader" x= "y=" 5 "Width=" 205 "height="/><text><! [Cdata[name]]></text></statictext><statictext><reportelement style= "PageHeader" x= "260" Y = "5" width= "255" height= "/><text><!" [Cdata[email]]></text></statictext><statictext><reportelement style= "PageHeader" x= "260" y= "5" width= "255" height= "/><text><!" [Cdata[qq]]></text></statictext></band></pageheader><detail><band height=] "><textfield><reportelement x=" 0 "y=" 4 "width=" height= ""/><textelement textAlignment= " Right "/><textfieldexpression><! [cdata[$F {Id}]]></textfieldexpression></textfield><textfield isstretchwithoverflow= "true" > <reportelement positiontype= "Float" x= "y=" 4 "width=" "" height=; <! [cdata[$F {Name}]]></textfieldexpression></textfield><textfield isStretchWithOverflow= "true" ><reportelement positiontype= "Float" x= "260" y= "4" width= "255" height= "/><textfieldexpression>" <! [cdata[$F {Email}]]></textfieldexpression></textfield><textfield isStretchWithOverflow= "true" ><reportelement positiontype= "Float" x= "260" y= "4" width= "255" height= "/><textfieldexpression>" <! [cdata[$F {qq}]]></textfieldexpression></textfield><line><reportelement positionType= " Float "x=" 0 "y=" width= "515" height= "1" forecolor= "#808080"/></line></band></detail></ Jasperreport>

Backstage Code: Testservlet.java

Package Servlets;import Java.io.file;import java.io.ioexception;import java.util.hashmap;import java.util.Map; Import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.http.HttpServlet; Import javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;import util. Jdbc;import Net.sf.jasperreports.engine.jrexporterparameter;import Net.sf.jasperreports.engine.jaspercompilemanager;import Net.sf.jasperreports.engine.jasperexportmanager;import Net.sf.jasperreports.engine.jasperfillmanager;import Net.sf.jasperreports.engine.jasperprint;import Net.sf.jasperreports.engine.jasperreport;import Net.sf.jasperreports.engine.export.jrhtmlexporter;import Net.sf.jasperreports.engine.export.jrhtmlexporterparameter;import Net.sf.jasperreports.engine.export.jrpdfexporter;import Net.sf.jasperreports.engine.export.jrxlsexporter;import Net.sf.jasperreports.engine.export.jrxlsexporterparameter;import Net.sf.jasperreports.engine.util.JRLoader; ImportNet.sf.jasperreports.view.jasperviewer;public class Testservlet extends HttpServlet {@Overrideprotected void service ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException {try{servletcontext context = This.getservletconfig (). Getservletcontext ();//jaspercompilemanager compilation manager//jasperfillmanager fill Manager// Jrxmlloader xml Loader//jasperprintmanager Print Manager//jasperexportmanager Export Manager jaspercompilemanager.compilereporttofile (Context.getrealpath ("/reports/dbreport.jrxml"));//compile Jrxml file, Generate Jasper File Map map=new HashMap ();//Parameter Mapmap.put ("UserName", "admin");//Generate Jrprint File// Jasperfillmanager.fillreporttofile (Context.getrealpath ("/reports/dbreport.jasper"), map, JDBC.conn); File Jasperfile=new file (Context.getrealpath ("/reports/dbreport.jasper"));            Jasperreport Jasperreport = (jasperreport) jrloader.loadobject (jasperfile); <span style= "White-space:pre" ></span>jasperprint jasperprint = Jasperfillmanager.fillreport (        Jasperreport, Map,jdbc.conn);   <span style= "White-space:pre" ></span>//generate HTML <span style= "White-space:pre" ></span>JRH            Tmlexporter html = new Jrhtmlexporter (); <span style= "White-space:pre" ></span>html.setparameter (Jrhtmlexporterparameter.jasper_print,            Jasperprint); <span style= "White-space:pre" ></span>html.setparameter (Jrhtmlexporterparameter.output_writer,            Response.getwriter ()); <span style= "White-space:pre" ></span>html.setparameter (jrhtmlexporterparameter.is_using_images_to_            Align,boolean.false); <span style= "White-space:pre" ></span>html.setparameter (jrexporterparameter.character_encoding, "            Utf-8 ");            <span style= "White-space:pre" ></span>html.exportreport ();            <span style= "White-space:pre" ></span>//generate Excel <span style= "White-space:pre" ></span>/* <span style= "White-space:pre" ></span>jrxlsexporter xls=new JRxlsexporter (); <span style= "White-space:pre" ></span>xls.setparameter (Jrxlsexporterparameter.jasper_print,           Jasperprint); <span style= "White-space:pre" ></span>xls.setparameter (Jrxlsexporterparameter.output_stream,            Response.getoutputstream ()); <span style= "White-space:pre" ></span>xls.setparameter (Jrxlsexporterparameter.is_one_page_per_sheet,            Boolean.false); <span style= "White-space:pre" ></span>xls.setparameter (Jrxlsexporterparameter.is_white_page_            BACKGROUND, Boolean.false); <span style= "White-space:pre" ></span>response.setheader ("Content-disposition", "attachment;filename=            First.xls ");            <span style= "White-space:pre" ></span>response.setcontenttype ("Application/vnd_ms-excel");            <span style= "White-space:pre" ></span>xls.exportreport (); <span style= "White-space:pre" ></span>*/<span style= "WHITE-SPACE:PRE "></span>//generate PDF <span style=" White-space:pre "></span>/* <span style=" White             -space:pre "></span>jrpdfexporter PDF = new Jrpdfexporter (); <span style= "White-space:pre" ></span>pdf.setparameter (Jrexporterparameter.jasper_print, JasperPrint)            ; <span style= "White-space:pre" ></span>pdf.setparameter (Jrexporterparameter.output_stream,                         Response.getoutputstream ()); <span style= "White-space:pre" ></span>response.setheader ("Content-disposition", "attachment;filename=            First.pdf ");            <span style= "White-space:pre" ></span>response.setcontenttype ("application/pdf");              <span style= "White-space:pre" ></span>response.setcharacterencoding ("UTF-8"); <span style= "White-space:pre" ></span>pdf.exportreport (); */}catch (Exception e) {e.printstacktrace ();}}} </span>
execution Effect: HTML data


The style of the page can be configured through the Jrxml file, handwriting is certainly not very good-looking, ireport is jasperreport visual tool, you can download ireport tools to design.

After the above code is saved, the code may have more <span> tags in front of it




Javaweb+jasperreport Report Development Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.