日期:2014-05-17 浏览次数:21042 次
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.Writer; import java.sql.Connection; import java.util.Map; import net.sf.jasperreports.engine.JRAbstractExporter; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.export.JExcelApiExporter; 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.JRPdfExporterParameter; import net.sf.jasperreports.engine.export.JRXlsExporterParameter; import net.sf.jasperreports.engine.util.JRLoader; import org.apache.log4j.Logger; import com.ssc.cba.report.ReportClient; import com.ssc.cba.service.util.DBUtil; import com.ssc.faw.util.GenException; public class JasperReportUtil { private static Logger logger = Logger.getLogger(JasperReportUtil.class); /** * export report * * @param jasperPrint * @param param * @return * @throws Exception */ public byte[] exportReport(JasperPrint jasperPrint, Map<String, Object> param) throws Exception { ByteArrayOutputStream oStream = new ByteArrayOutputStream(); JRAbstractExporter exporter = fillJasperPrint(jasperPrint, param); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, oStream); logger.info("[JasperReportUtil - exportReport] Exporting report starting..."); long startTime = System.currentTimeMillis(); exporter.exportReport(); long endTime = System.currentTimeMillis(); logger.info("[JasperReportUtil - exportReport] Exporting report end. It takes " + (endTime - startTime) + " ms"); byte[] bytes = oStream.toByteArray(); return bytes; } /** * fill the data into jasper print object * * @param jasperPrint * @param param * @return * @throws Exception */ public static JRAbstractExporter fillJasperPrint(JasperPrint jasperPrint, Map<String, Object> param) throws Exception { try { String mode = (String) param.get(ReportClient.REPORT_TYPE); logger.info("[JasperReportUtil - fillJasperPrint] The type of exported report is " + mode); JRAbstractExporter exporter = getJRExporter(mode); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); return exporter; } catch (Exception e) { logger.error("[JasperReportUtil - fillJasperPrint] Error occurs when filling Jasper report: " + e); throw new GenException(e); } } /** * export report * * @param param * @return * @throws GenException */ public byte[] exportReport(Map<String, Object> param) throws GenException { byte[] bytes = null; Connection conn = null; try { conn = DBUtil.getConnection(); JasperPrint jasperPrint = getJasperPrint(param, conn); bytes = exportReport(jasperPrint, param); } catch (Exception e) { logger.error("[JasperReportUtil - exportReport] Error occurs when exporting Jasper report: " + e); throw new GenException(e); } finally { DBUtil.closeConnection(conn); } return bytes; } /** * retrieve the Jasper exporter * * @param docType * @return */ public static JRAbstractExporter getJRExporter(String exportType) throws GenException { JRAbstractExporter exporter = null; if (ReportClient.EXPORT_PDF.equalsIgnoreCase(exportType)) { exporter = new JRPdfExporter(); configurePdfExporter(exporter); } else if (ReportClient.EXPORT_EXCEL.equalsIgnoreCase(exportType)) { exporter = new JExcelApiExporter(); configureXlsExporter(exporter); } else if (ReportClient.EXPORT_HTML.equalsIgnoreCase(expo