此處使用了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