iReport报表实战-图文详解

陈科肇

1.设计报表

官网地址:http://community.jaspersoft.com/

在开始之前设计报表之前,我们总得有工具来设计报表吧,这时我们就可以在官网地址里查找并下载

安装完报表设计工具后,启动工具

数据源:数据源有两种,JDBC数据源和List数据源。

首先,我们使用的是List数据源,也就是说是通过后台SQL语句查询到值后,封装到List集中,再将List集的数据传给报表充当数据源。

使用List集做为数据源的好处:报表设计的数据和SQL语句没有直接的关联,也可以说,这样比较安全。

a.配置要显示的字段

Fields->右键->...,可参照下图


b.接收传递过来的参数

Parameters->右键->添加,配置相关属性,即可!


c.添加子报表


注:

1.可以右键设置子报表的相关参数(属性);

2.如果要为子报表传入List集做为数据源,选中子报表->展开属性窗口->

设置connection type:Use a datasource expression,

设置Data Source Expression:newnet.sf.jasperreports.engine.data.JRBeanCollectionDataSource($P{sub1Ds})。

其中sub1Ds是接收传递过来的List集。


d.添加图片显示

组件面板->Image,再配置图片相关属性

比如:要显示图片的路径的属性Image Expression设置为$P{imageUrl}


2.集成到WEB项目并加载显示

@Controller层-EntryBillController.java

/**
	 * 打印pdf报表
	 * ckz
	 * @param modelandview
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/doPdf")
	public void doReportPdf(String billcode,String where,
		HttpServletRequest req,HttpServletResponse resp) throws Exception{
		entrybillService.doReport(billcode,where,req,resp);
	}
@Service层-EntryBillService.java

/**
	 * 打印报表
	 * ckz
	 * 
	 * @param billcode
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Transactional(readOnly=true)
	public void doReport(String billcode,String where, HttpServletRequest req,
			HttpServletResponse resp){
		try{
			List data = null;// = entrybillDao.getPrintEntryBillDs(billcode);
			List list_sub1 = null;//entrybillDao.getPrintSub1Ds(billcode);
			List list_sub2 = null;//entrybillDao.getPrintSub2Ds(billcode);
			//获取工程路径
			String root_path=req.getSession().getServletContext().getRealPath("");
			//获取.jasper文件路径
			String reportFilePath = root_path;//+"\\webresource\\reports\\report_entrybill_print_look_all_cn.jasper";
			//报表logo图片路径
			String imageUrl=root_path+"\\webresource\\reports\\xxx.png";
			//设置report参数
			Map params = new HashMap();
			String username = (String) req.getSession().getAttribute("employeename");
			params.put("username", username);
			//++++++++++
			data = entrybillDao.getPrintEntryBillDs(billcode);
			list_sub1 = entrybillDao.getPrintSub1Ds(billcode);
			list_sub2 = entryBillBinDao.getPrintSub2Ds(billcode);
			reportFilePath+="\\webresource\\reports\\entrybill\\report_entrybill_print_look_all_cn.jasper";
			params.put("sub1Ds", list_sub1);
			params.put("sub2Ds", list_sub2);
			params.put("entrybillmasTitle", "入库单详细表");
			params.put("SUBREPORT_DIR", root_path+"\\webresource\\reports\\entrybill\\");
			//++++++++++
			//获取数据源
			JRDataSource dataSource = new JRBeanCollectionDataSource(data);
			params.put("imageUrl", imageUrl);
			Map<String,Object> map = (Map) data.get(0);
			if("1".equals(map.get("BILLSTATE").toString())){
				params.put("billStateImage", root_path+"\\webresource\\reports\\audit-yes.png");
			}
			if("1".equals(map.get("DISUSESTATE").toString())){
				params.put("billStateImage", root_path+"\\webresource\\reports\\disuse-yes.png");
			}
			//获取jasperPrint对象
			JasperPrint jasperPrint = ReportUitl.getJasperPrint(reportFilePath, params, dataSource);
			ReportUitl.exportPdf(req, resp, jasperPrint);
		}catch(Exception ex){
			PrintWriter out = null;
			try {
				resp.setCharacterEncoding("UTF-8");
				out = resp.getWriter();
				out.write("<h1 style='position: absolute;left: 50%;top: 50%;margin-left: -180px;margin-top: -10px;'>打印报表出错,请重试!</h1>");
			} catch (IOException e) {
				e.printStackTrace();
			}finally{
				out.close();
			}
			ex.printStackTrace();
		}
		
		resp.setCharacterEncoding("UTF-8");
		resp.setContentType("text/html;charset=UTF-8");
		
	}
其中,exportPdf(req, resp, jasperPrint)及getJasperPrint(reportFilePath, params, dataSource)方法的类,ReportUitl.java

package com.wms.common;

import java.io.File;
import java.io.IOException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.util.JRLoader;

/**
 * 实现打印报表的常用方法
 * @author ckz
 *
 */
@SuppressWarnings("deprecation")
public class ReportUitl {
	/**
	 * 获取 JasperPrint 对象
	 * ckz
	 * 
	 * @return
	 * @throws Exception 
	 */
	public static JasperPrint getJasperPrint(String reportFileName,Map<String, Object> params,JRDataSource dataSource) throws Exception{
		File file = new File(reportFileName);
		if(!file.exists())
			throw new Exception("系统找不文件  " + reportFileName);
		JasperReport report = (JasperReport) JRLoader.loadObjectFromFile(file.getPath());
		JasperPrint print = JasperFillManager.fillReport(report, params, dataSource);
		return print;
	}
	
	/**
	 * 打印pdf文件
	 * ckz
	 * 
	 * @param req
	 * @param resp
	 * @param jasperPrint
	 * @throws IOException 
	 * @throws JRException 
	 */
	public static void exportPdf(HttpServletRequest req,HttpServletResponse resp,JasperPrint jasperPrint) throws Exception{
		//获取JasperPrint流 对象
		JasperPrint print = jasperPrint;
		//使用pdf导出器
		JRPdfExporter exporter =new JRPdfExporter();
		//设置exporter的参数
		exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
		exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, resp.getOutputStream());
		exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");
		//执行exporter
		exporter.exportReport();
		
	}
}
@Repository层-xxx.java

在这里,你向数据库检索的是你在报表中设置Fields的字段的数据List集

前台调用:
你只须访问@Controller层-EntryBillController.java类的方法

doReportPdf(String billcode,String where,HttpServletRequest req,HttpServletResponse resp)即可显示你设计的报表内容!

注:其中的*.jasper文件,是通过报表器工具生成的,点击Preview选项即可生成位于同目录(相对于当前编译的*.jrxml文件)下的*.jasper文件


  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值