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); } } }
详细解决方案
Jasper Util 种
热度:1100 发布时间:2012-08-21 13:00:21.0
相关解决方案
- java.lang.NoSuchMethodError: org.springframework.util.ReflectionUtils.makeAccess,该如何解决
- org.apache.jasper.JasperException: Unable to compile class for JSP:该怎么解决
- org.apache.jasper.JasperException: Unable to compile class for JSP:解决思路
- jsp 页面 安插ArrayList 报错。为什么,已经加了import="java.util."了呀!
- java.util.ArrayList cannot be cast to com.hxll.hr.entity.SalaryStandard解决方法
- 项目打包后报java.lang.NoClassDefFoundError: com/sun/tools/javac/util/List错误是咋回事
- java.util.Date转 json有关问题
- org.apache.jasper.JasperException: java.lang.NullPointerException,该怎么处理
- java.lang.ClassCastException: java.util.ArrayList解决思路
- java.util.vector中的vector的详细用法解决方法
- org.apache.tomcat.util.http.Parameters processParameters,该怎么解决
- ireporter + jasper 报错,该如何处理
- 请问:org.apache.jasper.JasperException: Cannot find any information on property 'userPhone' in a bean of type 'Bean.Sender'
- 求帮助JSF有关问题:org.apache.jasper.JasperException: java.lang.NullPointerException
- 对 Date的引用不明确,java.util中的类java.util.Date和java.sql中的类java.sql.Date都匹配,该如何处理
- simpleDateFormat 转换 java.util.Date解决方法
- cannot be cast to java.util.Collection解决方法
- org.apache.jasper.JasperException: java.lang.NumberFormatException: null,该怎么解决
- org.apache.jasper.JasperException: java.lang.NumberFormatException: null,该如何解决
- org.apache.jasper.JasperException: Failed to load or instantiate TagLibraryV
- 关于java.util.List的一个有关问题
- java.util.Timer,该如何处理
- 出错了啊org.apache.jasper.JasperException: /registerSucc.jsp(14,5) Invalid standar解决办法
- java.lang.ClassNotFoundException: org.apache.jasper.runtime.JspSourceDependent解决思路
- Caused by: java.util.jar.JarException: Cannot
- ~关于java.util.Calendar.setTime(Unknown Source)的有关问题 !
- 急java.util.ConcurrentModificationException错误有关问题
- 在java.util.Date中,怎么比较两个Date的大小,精确到分
- java.util.List 求详解,该如何解决
- J# 中有没有java.util.Arrays.equals(byte[],byte[])这个步骤