---
title: "jsp页面嵌入ireport报表"
author: "cookieandsession"
date: "2013-08-15T15:32:00Z"
tags: []
categories: ["行业应用"]
slug: "jsp-ireport"
draft: false
lastmod: "2013-08-15T16:18:48Z"
id: 1925000
url: "https://cookieandsession.iteye.com/blog/1925000"
---

以下方法生成报表数据(<span style="color:red">reportContent</span>)，也就是html中table代码，我们可以在servlet中调用以下方法生成报表数据，存到request域中，转发到jsp页面，显示报表数据。另外这个方法将jasperPrint对象存到了session中，这样导出Excel、Pdf时可以拿到这个对象，调用导出相关方法进行操作。


```java
private String generateReport(Map<String, Object> parameters, File reportFile, Connection connection, JRDataSource dataSource) {
		try {
			StringBuffer reportContent = new StringBuffer();
			JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath());
			JasperPrint jasperPrint = null;
			
			if (connection != null) {
				jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection);
			} else {
				jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
			}
			request.getSession().setAttribute("jasperPrint", jasperPrint);
			JRHtmlExporter exporter = new JRHtmlExporter();
	
			exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, reportContent);
			exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, Boolean.TRUE);
	
			exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);
			exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8");
			exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "");
			exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "");
			exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "");
	
			exporter.exportReport();
	
			return reportContent;
		} catch(Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (connection != null) {
				DBManager.relaseConnection(connection, null, null);
			}
		}
	}

```