iReport与JasperReport简介(二)
1.iReport 报表工具类
package com.runan.util.iReport; /** * iReport 报表工具 */ public class IreportUtils { /** * 获得JasperPrint对象;自定义填充报表时的parameter和dataSource. * @param filePath 模板路径 * @param parameter 参数 * @param dataSource 数据源(list,object[],map) */ public JasperPrint getJasperPrint(String filePath, Map parameter, JRDataSource dataSource) throws JRException { JasperReport jasperReport = null; try { //jasperReport = (JasperReport) JRLoader.loadObject(filePath); jasperReport = (JasperReport) JRLoader.loadObjectFromFile(filePath); return JasperFillManager.fillReport(jasperReport, parameter, dataSource); } catch (JRException e) { e.printStackTrace(); } return null; } /** * 获得JasperPrint对象;自定义填充报表时的parameter和connection * @param filePath 模板路径 * @param parameter 参数 * @param conn 数据库连接 */ public JasperPrint getJasperPrint(String filePath, Map parameter, Connection conn) throws JRException { JasperReport jasperReport = null; try { //jasperReport = (JasperReport) JRLoader.loadObject(filePath); jasperReport = (JasperReport) JRLoader.loadObjectFromFile(filePath); return JasperFillManager.fillReport(jasperReport, parameter, conn); } catch (JRException e) { e.printStackTrace(); } return null; } /** * 通过传入List类型数据源获取JasperPrint实例 * @param filePath 模板路径 * @param parameter 参数 * @param list 数据源 */ public JasperPrint getPrintWithBeanList(String filePath, Map parameter, List list) throws JRException { JRDataSource dataSource = new JRBeanCollectionDataSource(list); return getJasperPrint(filePath, parameter, dataSource); } /** * 通过传入List类型数据源获取JasperPrint实例 * @param filePath 模板路径 * @param parameter 参数 * @param Objcet[] 数据源 */ public JasperPrint getPrintWithBeanArayy(String filePath, Map parameter, Object[] objArray) throws JRException { JRDataSource dataSource = new JRBeanArrayDataSource(objArray); return getJasperPrint(filePath, parameter, dataSource); } /** * 传入类型,获取输出器 * * @param docType * @return */ public JRAbstractExporter getJRExporter(DocType docType) { JRAbstractExporter exporter = null; switch (docType) { case PDF: exporter = new JRPdfExporter(); break; case HTML: exporter = new JRHtmlExporter(); break; case XLS: exporter = new JExcelApiExporter(); break; case XML: exporter = new JRXmlExporter(); break; case RTF: exporter = new JRRtfExporter(); break; case CSV: exporter = new JRCsvExporter(); break; case TXT: exporter = new JRTextExporter(); break; case DOC: exporter = new JRRtfExporter(); break; } return exporter; } /** * 获得相应类型的Content type * @param docType * @return */ public String getContentType(DocType docType){ String contentType="text/html"; switch(docType){ case PDF: contentType = "application/pdf"; break; case XLS: contentType = "application/vnd.ms-excel"; break; case XML: contentType = "text/xml"; break; case RTF: contentType = "application/rtf"; break; case CSV: contentType = "text/plain"; break; case DOC: contentType = "application/msword;charset=utf-8"; break; } return contentType; } /** * 编译报表模板文件jrxml,生成jasper二进制文件 * @param jrxmlPath 报表设计文件 * @param jrsperPath 报表编译后文件 * @throws JRException */ public void complieJrxml(String jrxmlPath, String jasperPath) throws JRException { JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath); } /** * 定义了报表输出类型,固定了可输出类型 */ public static enum DocType { PDF, HTML, XLS, XML, RTF, CSV, TXT, DOC } /** * 输出html静态页面,必须注入request和response * @param jasperPath * @param params * @param sourceList * @param imageUrl * 报表文件使用的图片路径,比如 ../image?image= * @param response * @throws Exception */ public void servletExportHTMLList(String jasperPath, Map params, List sourceList, String imageUrl,HttpServletResponse response) throws Exception{ response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); JRAbstractExporter exporter = getJRExporter(DocType.HTML); JasperPrint jasperPrint = getPrintWithBeanList(jasperPath, params, sourceList); PrintWriter out = response.getWriter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageUrl); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);//不显示px exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.FALSE); exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "<br style='page-break-before:always;'>"); exporter.exportReport(); } /** * 输出html静态页面,必须注入request和response * @param jasperPath * @param params * @param sourceList * @param imageUrl * 报表文件使用的图片路径,比如 ../image?image= * @param response * @throws Exception */ public void servletExportHTMLArray(String jasperPath, Map params, Object[] sourceArray, String imageUrl,HttpServletResponse response) throws Exception{ response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); JRAbstractExporter exporter = getJRExporter(DocType.HTML); JasperPrint jasperPrint = getPrintWithBeanArayy(jasperPath, params, sourceArray); PrintWriter out = response.getWriter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageUrl); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);//不显示px exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.FALSE); exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "<br style='page-break-before:always;'>"); exporter.exportReport(); } /** * 输出html静态页面,必须注入request和response * @param jasperPath * @param params * @param sourceList * @param imageUrl * 报表文件使用的图片路径,比如 ../image?image= * @param response * @throws Exception */ public void servletExportHTML(String jasperPath, Map params, Connection con, String imageUrl, HttpServletResponse response) throws Exception{ response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); JRAbstractExporter exporter = getJRExporter(DocType.HTML); JasperPrint jasperPrint = getJasperPrint(jasperPath, params, con); PrintWriter out = response.getWriter(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER, out); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, imageUrl); exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,false);//不显示px exporter.setParameter(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR, Boolean.FALSE); exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, "<br style='page-break-before:always;'>"); exporter.exportReport(); } /** * 生成不同格式报表文档 * @param docType 文档类型 * @param jasperPath * @param params * @param sourceList * @param fileName * @param response */ public void exportDocumentList(DocType docType, String jasperPath, Map params, List sourceList, String fileName, HttpServletResponse response) throws Exception{ if (docType == DocType.HTML) { servletExportHTMLList(jasperPath, params, sourceList, fileName, response); return; } JRAbstractExporter exporter = getJRExporter(docType); // 获取后缀 String ext = docType.toString().toLowerCase(); if (!fileName.toLowerCase().endsWith(ext)) { fileName += "." + ext; } // 判断资源类型 if (ext.equals("xls")) { // 删除记录最下面的空行 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除多余的ColumnHeader exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); // 显示边框 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); } response.setContentType(getContentType(docType)); response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\""); exporter.setParameter(JRExporterParameter.JASPER_PRINT, getPrintWithBeanList(jasperPath, params, sourceList)); OutputStream outStream = null; PrintWriter outWriter = null; if(ext.equals("csv")){//解决中文乱码问题 response.setCharacterEncoding("GBK"); outWriter = response.getWriter(); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,outWriter); }else{ outStream = response.getOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outStream); if(ext.equals("txt")){ exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(80)); exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(25)); exporter.setParameter(JRTextExporterParameter.CHARACTER_ENCODING,"GBK"); } } try { exporter.exportReport(); } catch (JRException e) { throw new Exception(e); } finally { if (outStream != null) { try { outStream.close(); } catch (IOException ex) { } } } } /** * 生成不同格式报表文档 * @param docType 文档类型 * @param jasperPath * @param params * @param sourceList * @param fileName * @param response */ public void exportDocumentArray(DocType docType, String jasperPath, Map params, Object[] sourceArray, String fileName, HttpServletResponse response) throws Exception{ if (docType == DocType.HTML) { servletExportHTMLArray(jasperPath, params, sourceArray, fileName, response); return; } JRAbstractExporter exporter = getJRExporter(docType); // 获取后缀 String ext = docType.toString().toLowerCase(); if (!fileName.toLowerCase().endsWith(ext)) { fileName += "." + ext; } // 判断资源类型 if (ext.equals("xls")) { // 删除记录最下面的空行 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除多余的ColumnHeader exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); // 显示边框 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); } response.setContentType(getContentType(docType)); response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\""); exporter.setParameter(JRExporterParameter.JASPER_PRINT, getPrintWithBeanArayy(jasperPath, params, sourceArray)); OutputStream outStream = null; PrintWriter outWriter = null; if(ext.equals("csv")){//解决中文乱码问题 response.setCharacterEncoding("GBK"); outWriter = response.getWriter(); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,outWriter); }else{ outStream = response.getOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outStream); if(ext.equals("txt")){ exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(80)); exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(25)); exporter.setParameter(JRTextExporterParameter.CHARACTER_ENCODING,"GBK"); } } try { exporter.exportReport(); } catch (JRException e) { throw new Exception(e); } finally { if (outStream != null) { try { outStream.close(); } catch (IOException ex) { } } } } /** * 生成不同格式报表文档 * @param docType 文档类型 * @param jasperPath 模板文件路径 * @param params 参数 * @param con 连接 * @param fileName 输出文件名 * @param response 相应对象 */ //public void exportDocumentCon(DocType docType, String jasperPath, Map params, Connection con, String fileName, HttpServletResponse response) throws Exception{ public void exportDocumentCon(DocType docType, String jasperPath, Map params, String fileName, HttpServletResponse response) throws Exception{ DataSource dataSource ; WebApplicationContext cont = WebApplicationContextUtils .getRequiredWebApplicationContext(RequestFilter.threadLocalRequest.get() .getSession().getServletContext()); dataSource = (DataSource)cont.getBean("dataSource"); Connection con = dataSource.getConnection(); if (docType == DocType.HTML) { servletExportHTML(jasperPath, params, con, fileName, response); return; } JRAbstractExporter exporter = getJRExporter(docType); // 获取后缀 String ext = docType.toString().toLowerCase(); if (!fileName.toLowerCase().endsWith(ext)) { fileName += "." + ext; } // 判断资源类型 if (ext.equals("xls")) { // 删除记录最下面的空行 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); // 删除多余的ColumnHeader exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); // 显示边框 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); } response.setContentType(getContentType(docType)); response.setHeader("Content-Disposition", "attachment; filename=\"" + URLEncoder.encode(fileName, "UTF-8") + "\""); exporter.setParameter(JRExporterParameter.JASPER_PRINT, getJasperPrint(jasperPath, params, con)); OutputStream outStream = null; PrintWriter outWriter = null; if(ext.equals("csv")){ //解决中文乱码问题 response.setCharacterEncoding("GBK"); outWriter = response.getWriter(); exporter.setParameter(JRExporterParameter.OUTPUT_WRITER,outWriter); }else{ outStream = response.getOutputStream(); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, outStream); if(ext.equals("txt")){ exporter.setParameter(JRTextExporterParameter.CHARACTER_WIDTH, new Integer(80)); exporter.setParameter(JRTextExporterParameter.CHARACTER_HEIGHT, new Integer(25)); exporter.setParameter(JRTextExporterParameter.CHARACTER_ENCODING,"GBK"); } } try { exporter.exportReport(); } catch (JRException e) { throw new Exception(e); } finally { if (outStream != null) { try { outStream.flush(); outStream.close(); } catch (IOException ex) { ex.printStackTrace(); } } } } public static DocType getEnumDocType(String docType){ DocType type = DocType.HTML; docType = docType.toUpperCase(); if(docType.equals("PDF")){ type = DocType.PDF; }else if(docType.equals("XLS")){ type = DocType.XLS; }else if(docType.equals("XML")){ type = DocType.XML; }else if(docType.equals("RTF")){ type = DocType.RTF; }else if(docType.equals("CSV")){ type = DocType.CSV; }else if(docType.equals("TXT")){ type = DocType.TXT; }else if(docType.equals("DOC")){ type = DocType.DOC; } return type; } /** * 预览不同格式报表文档 * @param docType 文档类型 * @param jasperPath * @param params * @param sourceList */ public void viewDocumentArray(String jasperPath, Map params, Object[] sourceArray) throws Exception{ try { JasperViewer.viewReport(getPrintWithBeanArayy(jasperPath, params, sourceArray)); } catch (JRException e) { throw new Exception(e); } finally { } } /** * 预览不同格式报表文档 * @param docType 文档类型 * @param jasperPath * @param params * @param sourceList */ public void viewDocumentList(String jasperPath, Map params, List sourceArray) throws Exception{ try { JasperViewer.viewReport(getPrintWithBeanList(jasperPath, params, sourceArray)); } catch (JRException e) { throw new Exception(e); } finally { } } }
2.Controller类
@RequestMapping(params = "report") public void report(String ddlx,HttpServletRequest request,HttpServletResponse response){ String idsArray[] = request.getParameterValues("id"); Transferwithin transferwithin = transferwithinService.getTransferwithinById(idsArray[0]); CommonConfig config = new CommonConfig(); config.setCode(transferwithin.getDdhszjq()); config.setGroupCode("JQ"); config = commonConfigService.getCommonConfigbyCode(config); IreportUtils ireportUtils = new IreportUtils(); String jasperPath="/report/ireportTemplete/zf_djtzs.jasper"; jasperPath = request.getSession().getServletContext().getRealPath(jasperPath); String fileName="罪犯调监通知书"; Map<String,Object> params= new HashMap<String, Object>(); try { params.put("title", "罪犯调动通知单 (存根)"); params.put("zihao", transferwithin.getDlxh()); params.put("content", " 因为改造需要,经研究同意将罪犯 "+transferwithin.getXm()+"等"+idsArray.length+"名分给"+config.getName()+",请于"+DateUtils.dateToString(transferwithin.getDdrq(), "yyyy年MM月dd日")+"前办理好交接手续。"); params.put("fmd", "附名单:"+transferwithinService.getZfXmString(idsArray)); params.put("name", "德州监狱狱政管理科"); params.put("makedate", DateUtils.dateToString(new Date(), "yyyy年MM月dd日")); ireportUtils.exportDocumentCon(DocType.PDF, jasperPath, params,fileName, response); } catch (Exception e) { throw new RunanException(e.getMessage()); } }
导出图见附件,当然PDF某些元素是靠iReport工具生成。
相关推荐
### JasperReport+iReport 报表开发手册核心知识点详解 #### 一、JasperReport与iReport概述 **JasperReport** 是一款强大的、灵活的报表生成工具,它能够展示丰富的页面内容,并支持将这些内容转换为PDF、HTML或...
它有一个相关的开源工程--IReport,这是一个图形化的辅 助工具,主要用于数据报表设计。JasperReports 完全是由 Java 写成,可以应用于 Java 的应用 程序,包括 J2EE,Web 应用程序中生成动态内容,它主要目的是辅助...
iReport是一款强大的报表设计工具,它是JasperReports项目的一部分,专门用于创建...提供的“iReport报表开发指南.pdf”文档将是你探索这一领域的宝贵资源,它会详细讲解每个步骤和技巧,帮助你成为报表设计的专家。
### IReport报表开发教程 #### 第一部分:使用iReport制作报表的详细过程(Windows环境下) ##### 1.1 前言 iReport是一款强大的报表设计工具,它可以帮助开发者快速地设计并生成复杂的报表。本章节将详细介绍如何...
本示例提供了IReport报表样式和Java实现iReport报表功能的代码。 本示例主要描述了Action中利用JavaBean的方式实现IReport报表数据传递的写法,供刚接触IReport的朋友们参考。
iReport 报表开发工具1.2.5 iReport 是为JasperReports Library和JasperReports Server设计的报表可视化设计器。Report是一个能够创建复杂报表的开源项目。它100%使用Java语言编写。是目前全球最为流行的开源报表...
本帮助手册将深入介绍如何利用iReport进行报表开发,旨在为初学者提供一个清晰的入门指南。 一、iReport简介 iReport是Jaspersoft公司推出的一款开源报表设计工具,基于Java,与JasperReports库紧密集成。它的用户...
iReport是一款强大的报表开发工具,尤其在Java应用领域中被广泛应用。它允许开发者设计复杂的报表,包括表格、图表、文本和图像,支持多种数据源,如数据库、XML、JSON等。iReport是JasperReports库的配套设计工具,...
以上是 ireport 报表开发中常见问题的部分解决方案。开发者在遇到问题时,除了查阅本指南,还可以参考 ireport 的官方文档、社区论坛和其他在线资源。持续学习和实践是掌握 ireport 的关键,希望这些信息能帮助...
iReport是一款专为JasperReports设计的开源报表生成工具,完全使用Java编写,可以在SourceForge上找到。JasperReports是一个强大的报表生成库,能够解析XML文件生成Jasper文件,然后结合动态数据,输出HTML、PDF等...
《iReport-JasperReports报表开发指南+操作手册》是一份详尽的资源,旨在帮助开发者深入理解和高效使用iReport和JasperReports这两个强大的报表工具。iReport是JasperReports的图形化设计工具,用于创建复杂的报表...
开发使用步骤(iReport4.1)... 2 4.1. JasperReport 和iReport的介绍... 2 4.1.1. JasperReport 简介... 3 4.1.2. iReport 简介... 3 4.1.2.1. iReport几个重要的概念... 3 4.1.2.2. iReport数据库连接的建立 ...
【如何使用IReport3.5.1来制作报表模板】 IReport是一款强大的报表设计工具,尤其在配合JasperReports库时,能帮助开发者创建复杂的报表模板。以下将详细介绍如何使用IReport 3.5.1来创建报表模板。 **一、安装...
iReport是一款强大的报表设计工具...通过熟练掌握iReport,你可以提高报表开发效率,打造出专业、美观的报表解决方案。这个用户手册将引导你逐步了解和掌握这些功能,无论你是新手还是经验丰富的开发者,都能从中受益。
jasperReport+ireport 报表开发小结总结了 JasperReports 的基本概念、特点、类库依赖、开发流程和 ireport 使用说明等内容,为读者提供了一个全面的了解 JasperReports 和 ireport 的报表开发指南。