码迷,mamicode.com
首页 > 编程语言 > 详细

javaWeb+JasperReport报表开发实例

时间:2015-03-13 10:56:23      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:pdf   javaweb   

在网上搜索了很多JasperReport报表开发的例子,基本上都是说的一些理论知识,没有一个直接用代码直观表达的,想必大家想学习jasperReport报表的开始,都希望能够直观的看到代码,研究出代码执行后的效果是什么,而不是一大堆的理论,说这些对于刚开始学的有毛用,还不如直接看代码,在代码中多写点注释来的快。

本文用的是JasperReport最新版本6.0

JasperReport官网:community.jaspersoft.com

web开发建议下载JasperReports library --> jasperreports-xxx-project 里面包含demo以及api


项目结构:

jar包:新手可以把下载下来的所有jar包都放进去,熟练之后可以根据自己所需加入相关jar包

WebRoot下:新建reports文件夹,用来存放jrxml、jasper文件;build.xml(放入进去之后会出现蚂蚁图标);

技术分享


jrxml文件代码,此代码是xml格式文件,可以自己手写,也可以安装 iReport设计编译成jasper文件,为了入门下面纯手写

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{}为填充参数,$V{}为值 ,$F{}为查询值-->
	<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="30">
			<staticText>
				<reportElement style="pageHeader"  x="0" y="5" width="55" height="15"/>
				<textElement textAlignment="Center"/>
				<text><![CDATA[ID]]></text>
			</staticText>
			<staticText>
				<reportElement style="pageHeader"  x="55" y="5" width="205" height="15"/>
				<text><![CDATA[Name]]></text>
			</staticText>
			<staticText>
				<reportElement style="pageHeader"  x="260" y="5" width="255" height="15"/>
				<text><![CDATA[Email]]></text>
			</staticText>
			<staticText>
				<reportElement style="pageHeader"  x="260" y="5" width="255" height="15"/>
				<text><![CDATA[QQ]]></text>
			</staticText>
		</band>
	</pageHeader>
	<detail>
		<band height="20">
			<textField>
				<reportElement x="0" y="4" width="50" height="15"/>
				<textElement textAlignment="Right"/>
				<textFieldExpression><![CDATA[$F{id}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement positionType="Float" x="55" y="4" width="200" height="15"/>
				<textFieldExpression><![CDATA[$F{name}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement positionType="Float" x="260" y="4" width="255" height="15"/>
				<textFieldExpression><![CDATA[$F{email}]]></textFieldExpression>
			</textField>
			<textField isStretchWithOverflow="true">
				<reportElement positionType="Float" x="260" y="4" width="255" height="15"/>
				<textFieldExpression><![CDATA[$F{qq}]]></textFieldExpression>
			</textField>

			<line>
				<reportElement positionType="Float" x="0" y="19" width="515" height="1" forecolor="#808080"/>
			</line>
		</band>
	</detail>
	
</jasperReport>


后台代码: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;
import net.sf.jasperreports.view.JasperViewer;

public class TestServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try{
			ServletContext context = this.getServletConfig().getServletContext();
			
			//JasperCompileManager 编译管理器
			//JasperFillManager 填充管理器
			//JRXmlLoader xml加载器
			//JasperPrintManager 打印管理器
			//JasperExportManager 导出管理器
			
			JasperCompileManager.compileReportToFile(context.getRealPath("/reports/DbReport.jrxml"));//编译jrxml文件,生成jasper文件
			
			Map map=new HashMap();//参数map
			map.put("userName", "admin");
			
			//生成jrprint文件
			//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>//生成html
          <span style="white-space:pre">		</span>JRHtmlExporter 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>//生成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>//生成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>
执行效果:html数据

技术分享


页面的样式是可以通过jrxml文件的配置的,手写肯定不太好看,iReport是jasperReport的可视化工具,可以下载 iReport工具来设计。

上面代码保存之后,代码前面可能会多出<span>标签




javaWeb+JasperReport报表开发实例

标签:pdf   javaweb   

原文地址:http://blog.csdn.net/xiaosheng_papa/article/details/44237251

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!