此处使用了Java BeanCollection作为DataSource。
JSP页面代码:
<%@ page errorPage="error.jsp" %> <%@ page import="reportDatasource.*" %> <%@ page import="net.sf.jasperreports.engine.*" %> <%@ page import="net.sf.jasperreports.engine.util.*" %> <%@ page import="net.sf.jasperreports.engine.export.*" %> <%@ page import="net.sf.jasperreports.j2ee.servlets.*" %> <%@ page import="java.util.*" %><%@ page import="java.io.*" %> <%@ page import="java.net.URLEncoder" %> <%@ page import="javax.servlet.*" %> <%@page language="java" contentType="application/x-msdownload" pageEncoding="UTF-8"%> <% String requestFileName = "LineReport";//报表的名称 String reportFileName = requestFileName +".jasper"; String fileDownName = "XXX报表"; Map parameters = new HashMap(); // parameters.put("ReportTitle", "Address Report"); //向报表传递参数 JasperPrint jasperPrint = JavaBeanListDataSoureFactory.createJasperPrintByJavaBeanList(reportFileName, parameters, LocationValueBeanFactory.getPageLoadTimeHistoryLineReportBeanCollection()); // 输出 PathClassPath classpath = new PathClassPath(); String stringpath = application.getRealPath("/reports/"+reportFileName); System.out.print("reportFileName="+stringpath); File sourceFile = new File(stringpath); long start = System.currentTimeMillis(); System.err.println("PDF creation time : " + (System.currentTimeMillis() - start)); // 下载 response.reset(); // response.setContentType("application/x-download"); String filedisplay = fileDownName+".pdf"; filedisplay = URLEncoder.encode(filedisplay,"UTF-8"); response.addHeader("Content-Disposition","attachment;filename=" + filedisplay); OutputStream outp = null; try { outp = response.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outp); outp.flush(); } catch(Exception e) { System.out.println("Error!"); e.printStackTrace(); } finally { if(outp != null) { outp.close(); outp = null; } // 如下处理 getOutputStream() has already been called for this response 异常 out.clear(); out = pageContext.pushBody(); } %>
其中:
LocationValueBeanFactory.getPageLoadTimeHistoryLineReportBeanCollection()
为返回一个实体的数组的List:
Arrays.asList( LocationValueBean [] data);
其中:
JavaBeanListDataSoureFactory.createJasperPrintByJavaBeanList ()
代码为:
//本类可能会用到的包:
//
import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Properties; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JRRuntimeException; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.export.JRHtmlExporter; import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; import net.sf.jasperreports.j2ee.servlets.*; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; //
//
public static JasperPrint createJasperPrintByJavaBeanList(String reportFileName,Map parameters,Collection collection) { String path = ""; Properties prop = new Properties(); try { InputStream in = JavaBeanListDataSoureFactory. class.getClassLoader().getResourceAsStream("report.properties"); prop.load(in); path = prop.getProperty("site.reportjasper.path"); in.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } JRHtmlExporter exporter = new JRHtmlExporter(); JasperPrint jasperPrint = null; // PathClassPath classpath = new PathClassPath(); // ReadConfigInfo conf = new ReadConfigInfo(); String reportFileNameResult = path+reportFileName; File reportFile = new File(reportFileNameResult); if (!reportFile.exists()) throw new JRRuntimeException("File "+reportFileName+" not found. The report design must be compiled first."); parameters.put("ReportTitle", "Address Report"); parameters.put("BaseDir", reportFile.getParentFile()); try { jasperPrint = JasperFillManager.fillReport( reportFileNameResult, parameters, new JRBeanCollectionDataSource(collection) ); } catch (JRException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jasperPrint; }
PS:博客园中的插入代码依然有问题啊,有的时候最上面一行会从代码块中跑出来,有的时候最下面一行会跑出来,我的环境:Win7 x64+Chrome 21.01180.89