日期:2014-05-16 浏览次数:20373 次
private String generateReport(Map<String, Object> parameters, File reportFile, Connection connection, JRDataSource dataSource) { try { StringBuffer reportContent = new StringBuffer(); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile.getPath()); JasperPrint jasperPrint = null; if (connection != null) { jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, connection); } else { jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource); } request.getSession().setAttribute("jasperPrint", jasperPrint); JRHtmlExporter exporter = new JRHtmlExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, reportContent); exporter.setParameter(JRXlsExporterParameter.IS_IGNORE_CELL_BORDER, Boolean.TRUE); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); exporter.setParameter(JRHtmlExporterParameter.CHARACTER_ENCODING, "utf-8"); exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, ""); exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, ""); exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, ""); exporter.exportReport(); return reportContent; } catch(Exception e) { throw new RuntimeException(e); } finally { if (connection != null) { DBManager.relaseConnection(connection, null, null); } } }