当前位置: 代码迷 >> ASP >> 开发者施用JasperReport――不同数据源之Map数据源
  详细解决方案

开发者施用JasperReport――不同数据源之Map数据源

热度:600   发布时间:2013-04-02 12:35:26.0
开发者使用JasperReport――不同数据源之Map数据源

前言

上一篇我们使用了JRResultSetDataSource数据源来与数据库进行交互,今天我们来介绍下如何使用Map类型的数据源。


正题

跟之前的一样,我们要生成报表需要以下几个步骤:

1.引入jar包,请看静态文本报表》。


2.新建报表模版:

由于我们这次是使用的Map数据源,其实就是报表的数据来自于这个Map对象,所以我们的报表跟前面的一样:

<?xmlversion="1.0" encoding="UTF-8"?>

<jasperReportxmlns="http://jasperreports.sourceforge.net/jasperreports"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreportshttp://jasperreports.sourceforge.net/xsd/jasperreport.xsd"

name="AircraftReport">

 

<fieldname="ID" class="java.lang.String" />

<fieldname="NAME" class="java.lang.String" />

<fieldname="SEX" class="java.lang.String" />

 

<pageHeader>

<bandheight="30">

<staticText>

<reportElementx="0" y="0" width="69" height="24"/>

<textElementverticalAlignment="Bottom"/>

<text>Id</text>

</staticText>

<staticText>

<reportElementx="140" y="0" width="79" height="24"/>

<text>Name</text>

</staticText>

<staticText>

<reportElementx="280" y="0" width="69" height="24"/>

<text>Sex</text>

</staticText>

</band>

</pageHeader>

 

<detail>

<bandheight="40">

<textField>

<reportElementx="0" y="0" width="69" height="24"/>

<textFieldExpressionclass="java.lang.String">

$F{ID}

</textFieldExpression>

</textField>                        

<textField>

<reportElementx="140" y="0" width="69" height="24"/>

<textFieldExpressionclass="java.lang.String">

$F{NAME}

</textFieldExpression>

</textField>

<textField>

<reportElementx="280" y="0" width="69" height="24"/>

<textFieldExpressionclass="java.lang.String">

$F{SEX}

</textFieldExpression>

</textField>

</band>

</detail>

</jasperReport>

 

3.编译报表模版,请看《静态文本报表》


4.编写servlet:

由于Map对象分为两种JRMapArrayDataSourceJRMapCollectionDataSource,下面将这两种形式都写出来:

1JRMapArrayDataSource:

packagecom.dan.servlet;

 

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.PrintWriter;

importjava.io.StringWriter;

importjava.sql.Connection;

importjava.sql.DriverManager;

importjava.sql.ResultSet;

importjava.sql.Statement;

importjava.util.HashMap;

importjava.util.Map;

 

importjavax.servlet.ServletException;

importjavax.servlet.ServletOutputStream;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

importnet.sf.jasperreports.engine.JRDataSource;

importnet.sf.jasperreports.engine.JRResultSetDataSource;

importnet.sf.jasperreports.engine.JasperRunManager;

importnet.sf.jasperreports.engine.data.JRMapArrayDataSource;

 

/**

 * 使用Map作为数据源

 * @author zdd

 *

 */

publicclass MapArrayDSReportServlet extends HttpServlet {

 

privateJRDataSource createReportDataSource(){

JRMapArrayDataSourcedataSource;

Map[]resportRows = initializeMapArray();

dataSource= new JRMapArrayDataSource(resportRows);

returndataSource;                

}

 

privateMap[] initializeMapArray(){

HashMap[]reportRows = new HashMap[4];

HashMaprow1Map = new HashMap();

HashMaprow2Map = new HashMap();

HashMaprow3Map = new HashMap();

HashMaprow4Map = new HashMap();

 

row1Map.put("ID","1");

row1Map.put("NAME","zhaodd");

row1Map.put("SEX","female");

 

row2Map.put("ID","2");

row2Map.put("NAME","zhaodd2");

row2Map.put("SEX","female");

 

row3Map.put("ID","3");

row3Map.put("NAME","zhaodd3");

row3Map.put("SEX","female");

 

row4Map.put("ID","4");

row4Map.put("NAME","zhaodd4");

row4Map.put("SEX","female");

 

reportRows[0]= row1Map;

reportRows[1]= row2Map;

reportRows[2]= row3Map;

reportRows[3]= row4Map;

 

returnreportRows;

}

 

@Override

protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)

throwsServletException, IOException {

 

ServletOutputStreamservletOutputStream = resp.getOutputStream();

 

InputStreamreportStream =getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/classes/reports/AircraftReport.jasper");

 

try{

JRDataSourcedataSource = createReportDataSource();

 

JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream,newHashMap(),dataSource );

 

resp.setContentType("application/pdf");

servletOutputStream.flush();

servletOutputStream.close();

}catch(Exceptione){

StringWriterstringWriter = new StringWriter();

PrintWriterprintWriter = new PrintWriter(stringWriter);

e.printStackTrace(printWriter);

resp.setContentType("text/plain");

resp.getOutputStream().print(stringWriter.toString());

}

}

 

}

 

 

2JRMapCollectionDataSource:

packagecom.dan.servlet;

 

importjava.io.IOException;

importjava.io.InputStream;

importjava.io.PrintWriter;

importjava.io.StringWriter;

importjava.util.ArrayList;

importjava.util.Collection;

importjava.util.HashMap;

 

importjavax.servlet.ServletException;

importjavax.servlet.ServletOutputStream;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

importnet.sf.jasperreports.engine.JRDataSource;

importnet.sf.jasperreports.engine.JasperRunManager;

importnet.sf.jasperreports.engine.data.JRMapCollectionDataSource;

 

/**

 * 使用Map作为数据源

 * @author zdd

 *

 */

publicclass MapCollectionDSReportServlet extends HttpServlet {

 

privateJRDataSource createReportDataSource(){

JRMapCollectionDataSourcedataSource;

CollectionresportRows = initializeMapArray();

dataSource= new JRMapCollectionDataSource(resportRows);

returndataSource;                

}

 

privateCollection initializeMapArray(){

ArrayListreportRows = new ArrayList();

HashMaprow1Map = new HashMap();

HashMaprow2Map = new HashMap();

HashMaprow3Map = new HashMap();

HashMaprow4Map = new HashMap();

 

row1Map.put("ID","1");

row1Map.put("NAME","zhaodd");

row1Map.put("SEX","female");

 

row2Map.put("ID","2");

row2Map.put("NAME","zhaodd2");

row2Map.put("SEX","female");

 

row3Map.put("ID","3");

row3Map.put("NAME","zhaodd3");

row3Map.put("SEX","female");

 

row4Map.put("ID","4");

row4Map.put("NAME","zhaodd4");

row4Map.put("SEX","female");

 

reportRows.add(row1Map);

reportRows.add(row2Map);

reportRows.add(row3Map);

reportRows.add(row4Map);

 

returnreportRows;

}

 

@Override

protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)

throwsServletException, IOException {

 

ServletOutputStreamservletOutputStream = resp.getOutputStream();

 

InputStreamreportStream =getServletConfig().getServletContext().getResourceAsStream("/WEB-INF/classes/reports/AircraftReport.jasper");

 

try{

JRDataSourcedataSource = createReportDataSource();

 

JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream,newHashMap(),dataSource );

 

resp.setContentType("application/pdf");

servletOutputStream.flush();

servletOutputStream.close();

}catch(Exceptione){

StringWriterstringWriter = new StringWriter();

PrintWriterprintWriter = new PrintWriter(stringWriter);

e.printStackTrace(printWriter);

resp.setContentType("text/plain");

resp.getOutputStream().print(stringWriter.toString());

}

}

 

}

 

5.配置web.xml,就是配置servlet。


6.运行项目

看一下我的运行结果:

1JRMapArrayDataSource

 

2JRMapCollectionDataSource:

 

小结:

其实大家都知道一般有Array形式就会有Collection的形式,这个也不例外,所以Map对象有JRMapArrayDataSourceJRMapCollectionDataSource这两种形式,接下来还会介绍其他形式的数据源例如实体对象的,xml形式的,cvs形式的等等。

1楼lfmilaoshi昨天 23:40
今天,就来了一个系列
  相关解决方案