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 data=xxxService.getData(params);
Map 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 data=xxxService.getData(params);
Map 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配置

 


	
		ImageServlet
		net.sf.jasperreports.j2ee.servlets.ImageServlet
	
	
		ImageServlet
		
		/report/image
	
 

 

 

你可能感兴趣的:(HTML,Servlet,jfreechart,Struts,Web)