jasperreport导出报表到html、pdf

 

JFreeChart需要包: jfreechart-1.0.3.jar jcommon-1.0.6.jargnujaxp.jar

JasperReport需要包:

commons-beanutils-1.7.jar;commons-collections-2.1.jar;commons-digester-1.7.jar;

commons-logging-1.0.2.jar;commons-logging-api-1.0.2.jar;itext-1.3.1.jar;

jasperreports-1.3.3.jar;jdt-compiler-3.1.1.jar;jxl-2.6.jar;png-encoder-1.5.jar;poi-2.0-final-20040126.jar

 

一、导出报表到html

 

InputStream inputStream = ServletActionContext.getServletContext().getResourceAsStream("/report/xxx.jasper");
Collection<xxxo> data=xxxService.getData(params);
Map<Object,Object> map=xxxService.getMap(startTime, endTime);

JRBeanCollectionDataSource dataSource=new JRBeanCollectionDataSource(data);
//填充报表
JasperPrint jasperPrint = JasperFillManager.fillReport(inputStream,map, dataSource);
//设置输出类型及报表的内置参数
response.setContentType("text/html");
OutputStream outputStream=response.getOutputStream();
ReportXHtmlExporter htmlExporter=new ReportXHtmlExporter(page,jasperPrint);
request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE,jasperPrint);
htmlExporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint);
htmlExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream);
if(jasperPrint.getPages().size()>1){
	htmlExporter.setParameter(JRExporterParameter.PAGE_INDEX, 1);
}
htmlExporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE);
htmlExporter.exportReport();
outputStream.flush();
outputStream.close();
 

二、导出到PDF

 

OutputStream outputStream=response.getOutputStream();
File reportFile = new File(ServletActionContext.getServletContext().getRealPath("/report/xxx.jasper"));
if (!reportFile.exists()){
	throw new JRRuntimeException("File dispatch_report.jasper not found.");
}
JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());

//javabean数据源
Collection<xxx> data=xxxService.getData(params);
Map<Object,Object> map=xxxService.getMap(startTime, endTime);
JRBeanCollectionDataSource dataSource=new JRBeanCollectionDataSource(data);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,map, dataSource);

JRPdfExporter pdfExporter=new JRPdfExporter();
pdfExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
pdfExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 
response.setContentType("application/pdf");
response.setCharacterEncoding("UTF-8");  
response.setHeader("Content-Disposition", "attachment; filename=\""  + URLEncoder.encode("XXX报表", "UTF-8") + ".pdf\""); 
pdfExporter.exportReport();
outputStream.flush();
outputStream.close();
 

三、web.xml配置

 

<!--JasperReport显示图片的,象素图片-->
	<servlet>
		<servlet-name>ImageServlet</servlet-name>
		<servlet-class>net.sf.jasperreports.j2ee.servlets.ImageServlet</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>ImageServlet</servlet-name>
		<!-- /report/image路径,report表示struts中的表空间(具体就看url请求的路径),image表示Action文件里配置image?image=问号前的image相同-->
		<url-pattern>/report/image</url-pattern>
	</servlet-mapping>
 

 

 

你可能感兴趣的:(html,Web,struts,servlet,jfreechart)