前言
上一篇我们讲解了实体对象数据源,一个很实用的功能。但是有时候我们会用这样的需求,有一个xml或者cvs文件,我们需要将它展现成报表形式,这个实现很简单,今天就来讲解JRXmlDataSource。
正题
跟之前的一样,我们要生成报表需要以下几个步骤:
1.引入jar包,请看《静态文本报表》 。
2.新建报表模版:
为了能够让我们的报表顺利的生成,我们需要对报表模版做一个小更改,在字段标签里面添加<fieldDescription>标签。
<?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.编译报表模版,请看《静态文本报表》。
4.编写xml、csv文件:
对于xml文件我们没有限制,只要是符合xml标签格式要求的即可。
1)我们新建一个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)再来建一个AircraftCsv.csv文件
ID,NAME,SEX 1,Rebecca1,female 2,Rebecca2,female 3,Rebecca3,female 4,Rebecca4,female 5,Rebecca5,female 6,Rebecca6,female 7,Rebecca7,female 8,Rebecca8,female
5.编写servlet:
1)读取xml文件的XmlDSReportServlet :
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; /** * 使用JRXmlDataSource 填充 * @author zdd * */ public class XmlDSReportServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 2174316885912562306L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletOutputStream 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 stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); resp.setContentType("text/plain"); resp.getOutputStream().print(stringWriter.toString()); } } }
2)读取csv文件的CsvDSReportServlet :
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; /** * 使用JRResultSetDataSource填充 * @author zdd * */ public class CsvDSReportServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 2174316885912562306L; @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ServletOutputStream servletOutputStream = resp.getOutputStream(); InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/classes/reports/AircraftReportWithDescription.jasper"); try{ JRCsvDataSource 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 stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); resp.setContentType("text/plain"); resp.getOutputStream().print(stringWriter.toString()); } } }
5.配置web.xml,就是配置servlet
6.运行项目
看一下我的运行结果:
1)读取xml文件:
2)读取csv文件:
小结:
xml和csv文件是我们经常见到的两种数据文件格式,JasperReport将其单独拿出来也说明了这点,其他形式的数据源还会继续给大家进行更新。