Developers use jasperreport-XML and CSV data sources of different data sources

Source: Internet
Author: User

Preface

In the previous article, we explained the object data source, a very practical function. However, sometimes we use this requirement. There is an XML or CVS file, and we need to present it as a report. This implementation is very simple. Today we will explain jrxmldatasource.

 

Question

As before, the following steps are required to generate a report:

1. Introduce the jar package. See static text report.


2. Create a report template:

To generate a report smoothly, we need to make a small change to the Report Template and add the <fielddescription> label in the field label.

 

<?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="AircraftReportWithDescription"><field name="ID" class="java.lang.String" ><fieldDescription><![CDATA[ID]]></fieldDescription></field><field name="NAME" class="java.lang.String"><fieldDescription><![CDATA[NAME]]></fieldDescription></field><field name="SEX" class="java.lang.String" ><fieldDescription><![CDATA[SEX]]></fieldDescription></field><pageHeader><band height="30"><staticText><reportElement x="0" y="0" width="69" height="24" /><textElement verticalAlignment="Bottom"/><text>Id</text></staticText><staticText><reportElement x="140" y="0" width="79" height="24" /><text>Name</text></staticText><staticText><reportElement x="280" y="0" width="69" height="24" /><text>Sex</text></staticText></band></pageHeader><detail><band height="40"><textField><reportElement x="0" y="0" width="69" height="24" /><textFieldExpression class="java.lang.String">$F{ID}</textFieldExpression></textField><textField><reportElement x="140" y="0" width="69" height="24" /><textFieldExpression class="java.lang.String">$F{NAME}</textFieldExpression></textField><textField><reportElement x="280" y="0" width="69" height="24" /><textFieldExpression class="java.lang.String">$F{SEX}</textFieldExpression></textField></band></detail></jasperReport>

 

 

 

3. compile the report template. See static text report.


4. Compile XML and CSV files:

There are no restrictions on XML files, as long as they comply with the XML tag format requirements.

 

1) Create an aircraftdata. xml:

 

<?xml version="1.0" encoding="UTF-8"?><AircraftData><aircraft><ID>1</ID><NAME>REBECCA1</NAME><SEX>FEMALE</SEX></aircraft><aircraft><ID>2</ID><NAME>REBECCA2</NAME><SEX>FEMALE</SEX></aircraft><aircraft><ID>3</ID><NAME>REBECCA3</NAME><SEX>FEMALE</SEX></aircraft><aircraft><ID>4</ID><NAME>REBECCA4</NAME><SEX>FEMALE</SEX></aircraft></AircraftData>

 

2. Create an aircraftcsv.csv file.

 

ID,NAME,SEX1,Rebecca1,female2,Rebecca2,female3,Rebecca3,female4,Rebecca4,female5,Rebecca5,female6,Rebecca6,female7,Rebecca7,female8,Rebecca8,female

 

 

5. Compile servlet:

1) xmldsreportservlet for reading XML files:

 

Package COM. dan. servlet; import Java. io. bufferedinputstream; import Java. io. ioexception; import Java. io. inputstream; import Java. io. printwriter; import Java. io. stringwriter; import Java. util. hashmap; import javax. servlet. servletexception; import javax. servlet. servletoutputstream; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import net. SF. jasperreports. engine. jasperrunmanager; import net. SF. jasperreports. engine. data. jrxmldatasource;/*** use jrxmldatasource to fill in * @ author zdd **/public class xmldsreportservlet extends httpservlet {/*****/Private Static final long serialversionuid = 2174425885912562306l; @ overrideprotected void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {servletoutputstream = resp. getoutputstream (); inputstream reportstream = getservletconfig (). getservletcontext (). getresourceasstream ("/WEB-INF/classes/reports/aircraftreportwithdescription. jasper "); try {jrxmldatasource xmldatasource = new jrxmldatasource (New bufferedinputstream (getservletconfig (). getservletcontext (). getresourceasstream ("/WEB-INF/classes/reports/aircraftdata. XML "),"/aircraftdata/aircraft "); jasperrunmanager. runreporttopdfstream (reportstream, servletoutputstream, new hashmap (), xmldatasource); servletoutputstream. flush (); servletoutputstream. close () ;}catch (exception e) {stringwriter = new stringwriter (); printwriter = new printwriter (stringwriter); E. printstacktrace (printwriter); resp. setcontenttype ("text/plain"); resp. getoutputstream (). print (stringwriter. tostring ());}}}

 

 

2) csvdsreportservlet for reading CSV files:

 

Package COM. dan. servlet; import Java. io. ioexception; import Java. io. inputstream; import Java. io. inputstreamreader; import Java. io. printwriter; import Java. io. stringwriter; import Java. util. hashmap; import javax. servlet. servletexception; import javax. servlet. servletoutputstream; import javax. servlet. HTTP. httpservlet; import javax. servlet. HTTP. httpservletrequest; import javax. servlet. HTTP. httpservletresponse; import net. SF. jasperreports. engine. jasperrunmanager; import net. SF. jasperreports. engine. data. jrcsvdatasource;/*** use jrresultsetdatasource to fill * @ author zdd **/public class csvdsreportservlet extends httpservlet {/***/Private Static final long serialversionuid = 2174425885912562306l; @ overrideprotected void doget (httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception {servletoutputstream = resp. getoutputstream (); inputstream reportstream = getservletconfig (). getservletcontext (). getresourceasstream ("/WEB-INF/classes/reports/aircraftreportwithdescription. jasper "); try {jrcsvdatasource = new jrcsvdatasource (New inputstreamreader (getservletconfig (). getservletcontext (). getresourceasstream ("/WEB-INF/classes/reports/aircraftcsv.csv"); jrcsvdatasource. setusefirstrowasheader (true); jasperrunmanager. runreporttopdfstream (reportstream, servletoutputstream, new hashmap (), jrcsvdatasource); resp. setcontenttype ("application/pdf"); servletoutputstream. flush (); servletoutputstream. close () ;}catch (exception e) {stringwriter = new stringwriter (); printwriter = new printwriter (stringwriter); E. printstacktrace (printwriter); resp. setcontenttype ("text/plain"); resp. getoutputstream (). print (stringwriter. tostring ());}}}

 

 

5. configuring web. xml means configuring Servlet

 

6. Run the project

Let's take a look at my running results:

1) read XML files:


2) read the CSV file:

Summary:

XML and CSV files are two data file formats that we often see. This is also described in jasperreport. Other data sources will be updated.

 

 

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.