使用JavaBean构造JasperReport子报表

使用JavaBean作为数据源构造JasperReport子报表样式如下:

 

构造我的JavaBean如下(get,set方法省略):

public class Person {

	private String name;

	private String age;

	private String sex;

	private String telephone;
}

 

public class Depart {
	private String subTitle;

	private String depart_name;

	private List<Person> personList = null;
}

 构造我们的Service:

 

public class DepartService {
	public List<Depart> getAllDepart() {
		List<Depart> departList = new ArrayList<Depart>();

		List<Person> personList = null;

		Depart depart = new Depart("测试一", "开发部");
		personList = new ArrayList<Person>();
		personList.add(new Person("小博", "22", "男", "123456"));
		personList.add(new Person("张三", "21", "男", "321456"));
		personList.add(new Person("李四", "24", "女", "654321"));
		personList.add(new Person("王五", "23", "男", "123456"));
		depart.setPersonList(personList);
		departList.add(depart);

		depart = new Depart("测试二", "研发部");
		personList = new ArrayList<Person>();
		personList.add(new Person("小博", "22", "男", "123456"));
		personList.add(new Person("李四", "24", "女", "654321"));
		depart.setPersonList(personList);
		departList.add(depart);

		departList.add(depart);
		return departList;
	}
}

 构造我们的Servlet:

public class JRPDFServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DepartService departMght = new DepartService();
		List<Depart> departList = departMght.getAllDepart();

		ServletContext servletContext = this.getServletConfig().getServletContext();
		File departJasperFile = new File(servletContext.getRealPath("/jasper/Main_Department_List.jasper"));
		// 设置子报表的路径
		String root_path = servletContext.getRealPath("/");
		root_path = root_path.replace("\\", "\\\\");
		root_path = root_path + "jasper\\\\";

		Map<String, String> parameters = new HashMap<String, String>();
		JasperPrint jasperPrint = null;
		try {
			JasperReport departJasperReport = (JasperReport) JRLoader
					.loadObject(departJasperFile.getPath());
			parameters.put("SUBREPORT_DIR", root_path);
			jasperPrint = JasperFillManager.fillReport(departJasperReport,
					parameters, new JRBeanCollectionDataSource(departList));

		} catch (JRException e) {
			e.printStackTrace();
		}
		if (null != jasperPrint) {
			FileBufferedOutputStream fbos = new FileBufferedOutputStream();
			JRPdfExporter exporter = new JRPdfExporter();
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, fbos);
			exporter
					.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			try {
				exporter.exportReport();
				fbos.close();
				if (fbos.size() > 0) {
					response.setContentType("application/pdf");
					response.setContentLength(fbos.size());
					ServletOutputStream ouputStream = response
							.getOutputStream();
					try {
						fbos.writeData(ouputStream);
						fbos.dispose();
						ouputStream.flush();
					} finally {
						if (null != ouputStream) {
							ouputStream.close();
						}
					}
				}
			} catch (JRException e1) {
				e1.printStackTrace();
			} finally {
				if (null != fbos) {
					fbos.close();
					fbos.dispose();
				}
			}
		}
	}
}

 

在web.xml配置servlet如下:

<servlet>
		<servlet-name>JRPDFServlet</servlet-name>
		<servlet-class>com.mengya.servlet.JRPDFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>JRPDFServlet</servlet-name>
		<url-pattern>/JRSubPDFServlet</url-pattern>
	</servlet-mapping>

 页面调用:

<h3 align="center"><a href="JRSubPDFServlet">子表报</a></h3>

 

主要是报表设计如下面附件图片。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值