日期:2014-05-16  浏览次数:20373 次

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

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);
			}
		}
	}