Jasper Util 类
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(exportType)){ exporter = new JRHtmlExporter(); configureHtmlExporter(exporter);}else{ throw new GenException("[JasperReportUtil - getJRExporter] The type of exported Jasper report doesn't exist!");}return exporter; } private static void configurePdfExporter(JRAbstractExporter exporter) {exporter.setParameter(JRPdfExporterParameter.CHARACTER_ENCODING, ReportClient.CHARACTER_ENCODING); } private static void configureXlsExporter(JRAbstractExporter exporter) {exporter.setParameter(JRXlsExporterParameter.CHARACTER_ENCODING, ReportClient.CHARACTER_ENCODING);exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE);exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); } private static void configureHtmlExporter(JRAbstractExporter exporter) {exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE);exporter.setParameter(JRHtmlExporterParameter.ZOOM_RATIO, Float.parseFloat("1.2"));exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, ReportClient.CHARACTER_ENCODING); } /** * retrieve the Jasper Print Object * * @param param * @param conn * @return * @throws GenException */ public static JasperPrint getJasperPrint(Map<String, Object> param, Connection conn) throws GenException {JasperPrint jasperPrint = null;try{ String jasperFile = (String) param.get(ReportClient.REPORT_NAME); if (jasperFile == null) {throw new GenException("[JasperReportUtil - getJasperPrint] The Jasper report template can't be found out!"); } JasperReport jasperReport = (JasperReport) JRLoader.loadObject(new File(jasperFile)); jasperPrint = JasperFillManager.fillReport(jasperReport, param, conn);}catch (JRException e){ logger.error("[JasperReportUtil - getJasperPrint] Error occurs when retrieving JasperPrint Object: " + e); throw new GenException(e);}return jasperPrint; } /** * preview html jasper report * * @param param * @param writer * @throws GenException */ public static void previewHtmlReport(Map<String, Object> param, Writer writer) throws GenException {Connection conn = null;try{ conn = DBUtil.getConnection(); JasperPrint jasperPrint = JasperReportUtil.getJasperPrint(param, conn); JRAbstractExporter exporter = JasperReportUtil.fillJasperPrint(jasperPrint, param); exporter.setParameter(JRHtmlExporterParameter.OUTPUT_WRITER, writer); logger.info("[JasperReportUtil - previewHtmlReport] Preview report starting..."); long startTime = System.currentTimeMillis(); exporter.exportReport(); long endTime = System.currentTimeMillis(); logger.info("[JasperReportUtil - previewHtmlReport] Preview report end. It takes " + (endTime - startTime) + " ms");}catch (Exception e){ logger.error("[JasperReportUtil - previewHtmlReport] Error occurs when previewing Html Report: " + e); throw new GenException(e);}finally{ DBUtil.closeConnection(conn);} }}