package com.bo.japser;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporter;
import net.sf.jasperreports.engine.JRExporterParameter;
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.JRCsvExporter;
import net.sf.jasperreports.engine.export.JRCsvExporterParameter;
import net.sf.jasperreports.engine.export.JRHtmlExporter;
import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
import net.sf.jasperreports.engine.export.JRPdfExporter;
import net.sf.jasperreports.engine.export.JRRtfExporter;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXmlExporter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* 类说明:生成报表类
* 创建者:liuhui 创建时间:2009-4-16下午01:12:04
* 修改者: 修改时间: 修改原因:
*/
public class JasperReportsResult implements JasperReportConstants {
private static final Log LOG = LogFactory.getLog(JasperReportsResult.class);
private String imageServletUrl; // 图片路径
private String contentDisposition = null;
private String documentName = null; //文件名
private String format = null; //文件格式
private String delimiter = null;
public JasperReportsResult(){
imageServletUrl = IMAGE_SERVLET_URL;
}
public JasperReportsResult(String documentName, String format ){
this.documentName = documentName;
this.format = format;
imageServletUrl = IMAGE_SERVLET_URL;
}
public JasperReportsResult(String documentName, String format, String contentDisposition, String delimiter ){
this.documentName = documentName;
this.format = format;
this.contentDisposition = contentDisposition;
this.delimiter = delimiter ;
imageServletUrl = IMAGE_SERVLET_URL;
}
/**
* 获取打印源
* @param stream 报表文件流
* @param params 报表参数%P{}
* @param ds 数据源
* @return JasperPrint 打印源
* @throws FileNotFoundException
* @throws SQLException
* @throws JRException
*/
public JasperPrint fillReport(InputStream stream, Map params, JRDataSource ds) throws FileNotFoundException, SQLException, JRException {
JasperPrint jasperPrint = null;
if (params == null) {
params = new HashMap();
}
if (ds != null) {
LOG.debug("Filling report use JRDataSource");
jasperPrint = JasperFillManager.fillReport(stream, params, ds);
}
return jasperPrint;
}
/**
* 获取打印源 (集合)
* @param stream 报表文件流
* @param params 报表参数%P{}
* @param printList 数据源 (集合)
* @return JasperPrint 打印源
* @throws FileNotFoundException
* @throws SQLException
* @throws JRException
*/
public JasperPrint fillReport(InputStream stream, Map params, Collection printList) throws FileNotFoundException, SQLException, JRException {
JasperPrint jasperPrint = null;
if (params == null) {
params = new HashMap();
}
if (printList != null) {
LOG.debug("Filling report use JRDataSource");
jasperPrint = JasperFillManager.fillReport(stream, params, new JRBeanCollectionDataSource(printList) );
}
return jasperPrint;
}
/**
* 生成报表 (数据源为集合)
* @param response
* @param request
* @param templateReportFile 报表文件名
* @param parameters 报表参数$P{}
* @param printList 数据源 (集合)
* @throws Exception
*/
public void doExecute(HttpServletResponse response,HttpServletRequest request, String templateReportFile, Map parameters, Collection printList) throws Exception {
InputStream fileStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(JasperReportConstants.REPORT_JASPER_PATH + templateReportFile);
if (fileStream==null){
throw new FileNotFoundException("not find reportFile [" + templateReportFile + "]!");
}
if (parameters == null) {
parameters = new HashMap();
}
byte[] output;
JasperPrint jasperPrint = fillReport(fileStream, parameters, printList);
// Export the print object to the desired output format
try {
if (contentDisposition != null || documentName != null) {
final StringBuffer tmp = new StringBuffer();
tmp.append((contentDisposition == null) ? "inline" : contentDisposition);
if (documentName != null) {
tmp.append("; filename=");
tmp.append(documentName);
tmp.append(".");
tmp.append(format.toLowerCase());
}
response.setHeader("Content-disposition", tmp.toString());
}
JRExporter exporter;
if (format.equals(FORMAT_PDF)) {
response.setContentType("application/pdf");
exporter = new JRPdfExporter();
} else if (format.equals(FORMAT_CSV)) {
response.setContentType("text/plain");
exporter = new JRCsvExporter();
} else if (format.equals(FORMAT_HTML)) {
response.setContentType("text/html");
Map imagesMap = new HashMap();
request.getSession(true).setAttribute("IMAGES_MAP", imagesMap);
exporter = new JRHtmlExporter();
exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, (new StringBuilder()).append(request.getContextPath()).append(imageServletUrl).toString());
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
request.getSession().setAttribute("net.sf.jasperreports.j2ee.jasper_print", jasperPrint);
} else if (format.equals(FORMAT_XLS)) {
response.setContentType("application/vnd.ms-excel");
exporter = new JRXlsExporter();
} else if (format.equals(FORMAT_XML)) {
response.setContentType("text/xml");
exporter = new JRXmlExporter();
} else if (format.equals(FORMAT_RTF)) {
response.setContentType("application/rtf");
exporter = new JRRtfExporter();
} else {
throw new ServletException("Unknown report format: " + format);
}
output = exportReportToBytes(jasperPrint, exporter);
} catch (JRException e) {
String message = "Error producing " + format + " report for uri " + templateReportFile;
LOG.error(message, e);
throw new ServletException(e.getMessage(), e);
}
response.setContentLength(output.length);
// Will throw ServletException on IOException.
writeReport(response, output);
}
/**
* 生成报表 (数据源为集合)
* @param response
* @param request
* @param templateReportFile 报表文件名
* @param parameters