maven支持
<dependency> <groupId>net.sf.jasperreports</groupId> <artifactId>jasperreports</artifactId> <version>5.6.0</version> </dependency> <!-- apahce poi --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-examples</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml-schemas</artifactId> <version>3.7</version> </dependency> <dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy-all</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itextasian</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>com.lowagie</groupId> <artifactId>itext</artifactId> <version>2.1.7</version> </dependency>
package com.db.utils; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.lang.reflect.Field; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; 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.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; import net.sf.jasperreports.engine.base.JRBaseReport; import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; import net.sf.jasperreports.engine.design.JasperDesign; 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.JRXlsExporterParameter; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.engine.xml.JRXmlLoader; import org.apache.commons.lang.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 实现web 报表打印的功能,可见参数带有HttpServletRequest request,HttpServletResponse response * 同时也支持非web打印 * @author xjt * */ public class JasperUtils { private static final Logger logger = LoggerFactory.getLogger(JasperUtils.class); public static final String PRINT_TYPE = "print"; public static final String PDF_TYPE = "pdf"; public static final String EXCEL_TYPE = "excel"; public static final String HTML_TYPE = "html"; public static final String WORD_TYPE = "word"; public static final String exportFileName="export"; private static final String REPORT_NAME = "reportName"; private static final String REPORT_TYPE = "reportType"; private static final String REPORT_PRINT = "jasperPrint"; private static String jasperReportPath = "";//报表的模板文件存放路径(相对classpath,通过spring注入) /** * jasperDesignMap作为一个缓存来存储编译后的JasperReport模板 */ private Map<String, JasperReport> jasperDesignMap = new ConcurrentHashMap<String, JasperReport>(); public void resetJasperDesignCache() { jasperDesignMap.clear(); } /** * 如果导出的是excel,则需要去掉周围的margin * * @param jasperReport * @param type */ private static void prepareReport(JasperReport jasperReport, String type) { logger.debug("The method======= prepareReport() start......................."); if ("excel".equals(type)) try { Field margin = JRBaseReport.class .getDeclaredField("leftMargin"); margin.setAccessible(true); margin.setInt(jasperReport, 0); margin = JRBaseReport.class.getDeclaredField("topMargin"); margin.setAccessible(true); margin.setInt(jasperReport, 0); margin = JRBaseReport.class.getDeclaredField("bottomMargin"); margin.setAccessible(true); margin.setInt(jasperReport, 0); Field pageHeight = JRBaseReport.class .getDeclaredField("pageHeight"); pageHeight.setAccessible(true); pageHeight.setInt(jasperReport, 2147483647); } catch (Exception exception) { } } /** * 导出Excel * * @param jasperPrint * @param outputFileName * @param request * @param response * @throws IOException * @throws JRException */ private static void exportExcel(JasperPrint jasperPrint,String outputFileName, HttpServletRequest request,HttpServletResponse response) throws IOException, JRException { logger.debug("执行导出excel The method======= exportExcel() start......................."); response.setContentType("application/vnd.ms-excel"); // 设置头文件信息 String defaultname = null; if (outputFileName.trim() != null && outputFileName != null&&outputFileName!="") { defaultname = outputFileName + ".xls"; } else { defaultname = exportFileName+".xls"; } String fileName = new String(defaultname.getBytes("gbk"), "utf-8"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); ServletOutputStream outputStream = response.getOutputStream(); JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 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); exporter.exportReport(); outputStream.flush(); outputStream.close(); } private static void exportExcel(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException { JRXlsExporter exporter = new JRXlsExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); 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); exporter.exportReport(); outputStream.flush(); outputStream.close(); } /** * 导出pdf,注意此处中文问题, 这里应该详细说:主要在ireport里变下就行了。看图 * 1)在ireport的classpath中加入iTextAsian.jar 2)在ireport画jrxml时,看ireport最左边有个属性栏。 * 下边的设置就在点字段的属性后出现。 pdf font name :STSong-Light ,pdf encoding :UniGB-UCS2-H */ private static void exportPdf(JasperPrint jasperPrint, String outputFileName, HttpServletRequest request, HttpServletResponse response) throws IOException, JRException { response.setContentType("application/pdf"); String defaultname = null; if (outputFileName.trim() != null && outputFileName != null&&outputFileName!="") { defaultname = outputFileName + ".pdf"; } else { defaultname = exportFileName+".pdf"; } String fileName = new String(defaultname.getBytes("GBK"), "utf-8"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); ServletOutputStream outputStream = response.getOutputStream(); JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream); outputStream.flush(); outputStream.close(); } /** * * @param reportPrint * 对应可显示报表设计文件在内存中的形式,也就是对应的*.jrprint文件。 * 这时可以通过JasperReports内建的报表查看器直接使用,也可以序列化后存储到磁盘上以后使用,更甚于通过网络发送给他处使用 * @param os * @throws JRException * @throws IOException */ private static void exportPdf(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException { JRPdfExporter exporter = new JRPdfExporter(); //设置JASPER_PRINT 为reportPrint的 jasperPrint exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); //设置输出流为OutputStream 目前处理未web response的输出流 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); outputStream.flush(); outputStream.close(); } /** * 导出html * @param jasperPrint * @param outputFileName * @param request * @param response * @param pageIndexStr * @throws IOException * @throws JRException */ private static void exportHtml(JasperPrint jasperPrint,String outputFileName, HttpServletRequest request,HttpServletResponse response, String pageIndexStr)throws IOException, JRException { response.setContentType("text/html"); ServletOutputStream outputStream = response.getOutputStream(); JRHtmlExporter exporter = new JRHtmlExporter(); Integer pageIndex = getPageIndex(jasperPrint, request, pageIndexStr); // 得到当前页码 if (pageIndex != null) { // 如果页码不为空 则设置分页页码 exporter.setParameter(JRExporterParameter.PAGE_INDEX, pageIndex); } exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8"); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); outputStream.flush(); outputStream.close(); } /** * * @param jasperPrint * @param outputStream * @throws JRException * @throws IOException */ private static void exportHtml(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException { JRHtmlExporter exporter=new JRHtmlExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); //exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "report/image?image="); //servletOutstream 打印网页边框图片显示不了,本地打印能够显示 ,web打印·html暂时隐藏 exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,Boolean.FALSE); exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "GBK"); exporter.exportReport(); outputStream.flush(); outputStream.close(); } /** * description:通过传进来的pageIndexStr * 得到当前页码 Date 2013-1-18 上午10:49:38 * @param @param jasperPrint * @param @param request * @param @param pageIndexStr * @param @return * @return Integer */ private static Integer getPageIndex(JasperPrint jasperPrint,HttpServletRequest request, String pageIndexStr) { if (pageIndexStr == null || StringUtils.isBlank(pageIndexStr)) { // 如果pageIndexStr为空或空字符串则返回null return null; } Integer pageIndex = 0; int lastPageIndex = 0; if (jasperPrint.getPages() != null) { // 得到最后一页的 页码 lastPageIndex = jasperPrint.getPages().size() - 1; } if ("lastPage".equals(pageIndexStr)) { // 如果字符串==lastPage // 则反lastPageIndex的值赋给pageIndex // 并返回pageIndex pageIndex = lastPageIndex; return pageIndex; } try { pageIndex = Integer.parseInt(pageIndexStr); if (pageIndex > 0) { // 从ireport // 传来的PageIndex是从1开始,而JRExporterParameter.PAGE_INDEX是从0开始的 pageIndex = pageIndex - 1; } } catch (Exception e) { e.printStackTrace(); } if (pageIndex < 0) { pageIndex = 0; } if (pageIndex > lastPageIndex) { pageIndex = lastPageIndex; } return pageIndex; } /** * 导出word * @param jasperPrint * @param outputFileName * @param request * @param response * @throws JRException * @throws IOException */ private static void exportWord(JasperPrint jasperPrint,String outputFileName, HttpServletRequest request,HttpServletResponse response) throws JRException, IOException { response.setContentType("application/msword;charset=utf-8"); String defaultname = null; if (outputFileName.trim() != null && outputFileName != null&&outputFileName!="") { defaultname = outputFileName + ".doc"; } else { defaultname = exportFileName+".doc"; } String fileName = new String(defaultname.getBytes("GBK"), "utf-8"); response.setHeader("Content-disposition", "attachment; filename=" + fileName); JRExporter exporter = new JRRtfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, response.getOutputStream()); exporter.exportReport(); } /** * * @param jasperPrint * @param outputStream * @throws JRException * @throws IOException */ private static void exportWord(JasperPrint jasperPrint, OutputStream outputStream) throws JRException, IOException { JRExporter exporter = new JRRtfExporter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT,jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outputStream); exporter.exportReport(); outputStream.flush(); outputStream.close(); } public static void export(Collection datas, String type,String outputFileName, InputStream jasperFileIS, HttpServletRequest request,HttpServletResponse response, String pageIndex) throws IOException { Map parameters = new HashMap(); export(datas, type, outputFileName, jasperFileIS,parameters, request, response, pageIndex); } /** * 按照类型导出不同格式文件 * @param datas * 需要导出的数据 * @param type * 导出文件类型 * @param inputStream * .jasper文件的来源 * @param request * @param response * @param outputFileName * 默认的导出文件的名称 * @throws IOException */ public static void export(Collection datas, String type,String outputFileName, InputStream jasperFileIS,Map<String, ?> parameters, HttpServletRequest request,HttpServletResponse response, String pageIndex) throws IOException { try { logger.info("inputStream==" + jasperFileIS); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFileIS); prepareReport(jasperReport, type); JRDataSource ds = new JRBeanCollectionDataSource(datas, false); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, (Map<String, Object>) parameters, ds); if (EXCEL_TYPE.equals(type)) { exportExcel(jasperPrint, outputFileName, request, response); } else if (PDF_TYPE.equals(type)) { exportPdf(jasperPrint, outputFileName, request, response); } else if (HTML_TYPE.equals(type)) { exportHtml(jasperPrint, outputFileName, request, response, pageIndex); } else if (WORD_TYPE.equals(type)) { exportWord(jasperPrint, outputFileName, request, response); } } catch (Exception e) { e.printStackTrace(); }finally{ if (jasperFileIS != null) { jasperFileIS.close(); } } } public static void export(Collection datas, String type,String outputFileName, JasperReport jasperReport, HttpServletRequest request,HttpServletResponse response, String pageIndex) { Map parameters = new HashMap(); export(datas, type, outputFileName, jasperReport, parameters, request, response, pageIndex); } /** * @param datas * 导出数据集合 * @param type * 导出文件类型 * @param outputFileName * 默认导出文件名 * @param jasperReport * 对应二进制报表文件在内存中的形式,也就是对应的*.jasper文件。 * @param request * @param response * @param pageIndex */ public static void export(Collection datas, String type,String outputFileName, JasperReport jasperReport,Map<String, ?> parameters, HttpServletRequest request,HttpServletResponse response, String pageIndex) { try { prepareReport(jasperReport, type); JRDataSource ds = new JRBeanCollectionDataSource(datas, false); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, (Map<String, Object>) parameters, ds); if (EXCEL_TYPE.equals(type)) { exportExcel(jasperPrint, outputFileName, request, response); } else if (PDF_TYPE.equals(type)) { exportPdf(jasperPrint, outputFileName, request, response); } else if (HTML_TYPE.equals(type)) { exportHtml(jasperPrint, outputFileName, request, response, pageIndex); } else if (WORD_TYPE.equals(type)) { exportWord(jasperPrint, outputFileName, request, response); } } catch (Exception e) { e.printStackTrace(); } } public static void export(Collection datas, String type,String outputFileName, String sourceFilePath, HttpServletRequest request,HttpServletResponse response, String pageIndex) { Map parameters = new HashMap(); export(datas, type, outputFileName, sourceFilePath, parameters, request, response, pageIndex); } /** * * @param datas * 导出数据集合 * @param type * 导出文件类型 * @param outputFileName * 默认导出文件名 * @param sourceFilePath * *.jasper文件路径 * @param request * @param response * @param pageIndex */ public static void export(Collection datas, String type,String outputFileName, String sourceFilePath,Map<String, ?> parameters, HttpServletRequest request,HttpServletResponse response, String pageIndex) { logger.debug("导出判断 The method======= export() start......................."); try { InputStream jasperFIleIS=new FileInputStream(new File(sourceFilePath)); if (jasperFIleIS==null) throw new RuntimeException("报表文件不存在:" + sourceFilePath); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFIleIS); //JRLoader 将*.jasper文件加载为JasperReport对象 prepareReport(jasperReport, type); JRDataSource ds = new JRBeanCollectionDataSource(datas, false); JasperPrint jasperPrint = JasperFillManager.fillReport( jasperReport, (Map<String, Object>) parameters, ds); if (EXCEL_TYPE.equals(type)) { exportExcel(jasperPrint, outputFileName, request, response); } else if (PDF_TYPE.equals(type)) { exportPdf(jasperPrint, outputFileName, request, response); } else if (HTML_TYPE.equals(type)) { exportHtml(jasperPrint, outputFileName, request, response, pageIndex); } else if (WORD_TYPE.equals(type)) { exportWord(jasperPrint, outputFileName, request, response); } } catch (Exception e) { e.printStackTrace(); } } /** * outputStream需要设置目的文件才能正常使用,否则报错或者没有格式 * @param jasperPrint * @param reportType * @param outputStream */ public static void export(JasperPrint jasperPrint, String reportType,OutputStream outputStream) { try { if (EXCEL_TYPE.equals(reportType)) { exportExcel(jasperPrint, outputStream); } else if (PDF_TYPE.equals(reportType)) { exportPdf(jasperPrint, outputStream); } else if (HTML_TYPE.equals(reportType)) { exportHtml(jasperPrint, outputStream); } else if (WORD_TYPE.equals(reportType)) { exportWord(jasperPrint, outputStream); } } catch (Exception e) { // TODO: handle exception } } /** * * @param jasperPrint * @param reportType * @param response * @throws IOException */ public static void export(JasperPrint jasperPrint, String reportType,HttpServletResponse response) throws IOException { export(jasperPrint, reportType, response.getOutputStream()); } /** * * @param model * @param request * @param response * @throws IOException */ public static void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws IOException { String reportName = (String) model.get(REPORT_NAME);//报表的文件名 String reportType = (String) model.get(REPORT_TYPE);//报表的格式pdf xls ..... JasperPrint jasperPrint=(JasperPrint) model.get(REPORT_PRINT);//这就是之前生成的中间文件 response.setContentType("application/x-msdown;charset=utf-8"); try { /* http头里的文件名貌似不支持utf-8,gbk之类的编码,需要转换一下 * 另外发现如果用new String(reportName.getBytes("UTF-8"), "iso-8859-1")的话Chrome和FF的 * 下载对话框的文件名是正常的,IE却是乱码,只能用GBK才正常 */ response.setHeader("Content-Disposition","attachment;filename=\"" + new String(reportName.getBytes("GBK"), "iso-8859-1") + "\""); } catch (UnsupportedEncodingException e) { logger.error(null, e); } export(jasperPrint, reportType, response); } /** * * @param exportType * 导出文件的类型 * @param jasperFileName * jasper文件的路径地址 * @param list * 导出的数据 * @param outputFileName * 默认的导出文件的名称 * @param request * @param response * @throws IOException */ public static void exportByJasper(String exportType, String jasperFilePath,List list, String outputFileName, HttpServletRequest request,HttpServletResponse response) throws IOException { exportByJasper(exportType, jasperFilePath, list, outputFileName, null,request,response); } /** * 导出入口 (分页显示 导出传入的pageIndex 页的数据) * @param exportType * 导出文件的类型 * @param jasperFileName * jasper文件的名字 如: xx.jasper 默认地址为classpath下的文件 * @param list * 导出的数据 * @param outputFileName * 默认的导出文件的名称 * @param pageIndex * 需要导出数据的页码 当pageIndex = null时导出所有数据 * @param request * @param response * @throws IOException */ public static void exportByJasper(String exportType, String jasperFilePath,List list, String outputFileName, String pageIndex,HttpServletRequest request, HttpServletResponse response) throws IOException { File file = new File(jasperFilePath); InputStream jasperFileIS = null; try { jasperFileIS = new FileInputStream(file); } catch (FileNotFoundException e) { e.printStackTrace(); } export(list, exportType, outputFileName, jasperFileIS, request, response,pageIndex); } /** * * @param exportType * @param JRxmlPath * @param list * @param outputFileName * @param pageIndex * @param request * @param response */ public static void exportByJRxml(String exportType, String JRxmlPath,List list, String outputFileName, String pageIndex,HttpServletRequest request, HttpServletResponse response){ File file = new File(JRxmlPath); InputStream JRxmlIS = null; JasperReport jasperReport = null; try { JRxmlIS=new FileInputStream(file); if (JRxmlIS==null) throw new RuntimeException("jrxml文件不存在:" + JRxmlPath); JasperDesign jasperDesign = JRXmlLoader.load(JRxmlIS); jasperReport = JasperCompileManager.compileReport(jasperDesign); export(list, exportType, outputFileName, jasperReport, request, response, pageIndex); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); }finally{ if (JRxmlIS != null) { try { JRxmlIS.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } /** * * @param reportKey * @return */ private JasperReport getJasperReport(final String reportKey) { try { return _getJasperReport(reportKey); } catch (IOException e) { logger.error(null, e); throw new RuntimeException("关闭文件流异常:" + reportKey); } catch (JRException e) { logger.error(null, e); throw new RuntimeException("产生报表异常:" + reportKey); } } /** * * @param reportKey * @return * @throws IOException * @throws JRException */ private JasperReport _getJasperReport(final String reportKey) throws IOException, JRException { JasperReport jasperReport = null; if (jasperDesignMap.containsKey(reportKey)) { jasperReport = jasperDesignMap.get(reportKey); } else { jasperReport = getJasperReportFromFile(reportKey); jasperDesignMap.put(reportKey, jasperReport); } return jasperReport; } /** * 通过读取*.jrxml的地址转为为InputStream流,默认地址为classpath下面的文件 * 编译生成JasperReport对象 * @param reportKey * @return * @throws IOException * @throws JRException */ public static JasperReport getJasperReportFromFile(final String reportKey) throws IOException, JRException { String filePath = jasperReportPath + reportKey;//图省事只支持jrxml的 InputStream jasperFileIS = null; JasperReport jasperReport = null; try { logger.info(filePath); logger.info(JasperUtils.class.getClassLoader().getResource(filePath).getPath()); jasperFileIS = JasperUtils.class.getClassLoader().getResourceAsStream(filePath); if (jasperFileIS == null) { throw new RuntimeException("报表文件不存在:" + filePath); } JasperDesign jasperDesign = JRXmlLoader.load(jasperFileIS); jasperReport = JasperCompileManager.compileReport(jasperDesign); } finally { if (jasperFileIS != null) { jasperFileIS.close(); } } return jasperReport; } /** * 通过读取*.jrxml文件为InputStream流,编译生成JasperReport对象 * @param jasperReportFileIS * @return JasperReport对象,对应二进制报表文件在内存中的形式,也就是对应的*.jasper文件。 * @throws IOException * @throws JRException */ public static JasperReport getJasperReport(final InputStream jasperReportFileIS) throws IOException, JRException{ JasperReport jasperReport = null; try { if (jasperReportFileIS == null) { throw new RuntimeException("报表文件不存在"); } JasperDesign jasperDesign = JRXmlLoader.load(jasperReportFileIS); jasperReport = JasperCompileManager.compileReport(jasperDesign); } finally { if (jasperReportFileIS != null) { jasperReportFileIS.close(); } } return jasperReport; } }