/** * 本类主要作用为:生成或者处理报表页面 */ package com.yitong.struts.common; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.sql.Connection; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JRParameter; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; 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.JRXlsExporter; import net.sf.jasperreports.engine.export.JRXlsExporterParameter; import net.sf.jasperreports.engine.util.JRLoader; import com.yitong.struts.dao.GetCon; import com.yitong.struts.dao.SQLConnection; public class RptProc { private static RptProc instance = null; private static String imagesSavePath = "/ereportDemo/reports/images/"; // 本工程中图像文件的存放路径,保证所要的图片存在 private RptProc() { } /** * 生成分页Html,该方法被procHtmlRpt()方法调用 * * @param pageIndex * 当前页数 * @param lastPageIndex * 总页数 * @param localUrl * 查看本报表的地址 * @param backUrl * 返回查询页面的地址 * @return 生成的Html */ public static String splitPage(int pageIndex, int lastPageIndex, String localUrl, String backUrl) { if (localUrl.indexOf("?") < 0) { localUrl = localUrl + "?1=1"; } System.out.println("\tlocalUrl:"+localUrl); StringBuffer sp = new StringBuffer( "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">"); sp.append("<tr>"); sp.append("<td><a href=\"" + localUrl + "&reload=true\"><img src=\"" + imagesSavePath + "reload.gif\" border=\"0\"></a></td>"); sp.append("<td> </td>"); System.out.println("\tpageIndex:"+pageIndex); if (pageIndex > 0) { sp.append("<td><a href=\"" + localUrl + "&page=0\"><img src=\"" + imagesSavePath + "first.GIF\" border=\"0\"></a></td>"); sp.append("<td><a href=\"" + localUrl + "&page=" + (pageIndex - 1) + "\"><img src=\"" + imagesSavePath + "previous.GIF\" border=\"0\"></a></td>"); } else { sp.append("<td><img src=\"" + imagesSavePath + "first_grey.GIF\" border=\"0\"></td>"); sp.append("<td><img src=\"" + imagesSavePath + "previous_grey.GIF\" border=\"0\"></td>"); } System.out.println("\tlastPageIndex:"+lastPageIndex); if (pageIndex < lastPageIndex) { sp.append("<td><a href=\"" + localUrl + "&page=" + (pageIndex + 1) + "\"><img src=\"" + imagesSavePath + "next.GIF\" border=\"0\"></a></td>"); sp.append("<td><a href=\"" + localUrl + "&page=" + (lastPageIndex) + "\"><img src=\"" + imagesSavePath + "last.GIF\" border=\"0\"></a></td>"); } else { sp.append("<td><img src=\"" + imagesSavePath + "next_grey.GIF\" border=\"0\"></td>"); sp.append("<td><img src=\"" + imagesSavePath + "last_grey.GIF\" border=\"0\"></td>"); } sp.append("<td width=\"100%\" align=\"right\">"); sp.append("<a href=\"" + localUrl + "&isSave=true\">"); sp.append("<img src=\"" + imagesSavePath + "Excel.gif\" border=\"0\">"); // 导出PDF sp.append("<a href=\"" + localUrl + "&isSavePDF=true\">"); sp.append("<img src=\"" + imagesSavePath + "pdf.gif\" border=\"0\">"); sp.append("</a>"); // 如果返回Url这个参数为空,则不显示后退按钮 if (!CFormat.isEmpty(backUrl)) { sp.append(" "); sp.append("<a href=\"" + backUrl + "\">"); sp.append("<img src=\"" + imagesSavePath + "back.gif\" border=\"0\">"); sp.append("</a>"); } sp.append("</td>"); sp.append(" </tr>"); sp.append("</table>"); return sp.toString(); } /** * 统一进行报表处理的函数---导出Html * * @param request * 从Action传递过来的request * @param response * 从Action传递过来的response * @param parameter * Map类型的参数集合 * @param reportFile * 报表真实路径 * @param localUrl * 展示本报表的地址 * @param backUrl * 返回的地址,即本报表查询地址 * @throws Exception */ public static void procHtmlRpt(HttpServletRequest request, HttpServletResponse response, Map parameter, String reportFile, String localUrl, String backUrl) throws Exception { Connection conn = null; // 获取Connection连接 GetCon con = new GetCon(); conn = con.getCon(); JasperReport jasperReport = (JasperReport) JRLoader.loadObject(reportFile); JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameter, conn); conn.close(); JRHtmlExporter exporter = new JRHtmlExporter(); // -------------------------控制分页的代码--------------------- int pageIndex = 0; int lastPageIndex = 0; if (jasperPrint.getPages() != null) { lastPageIndex = jasperPrint.getPages().size() - 1; } if (lastPageIndex < 0) { lastPageIndex = 0; } String pageStr = request.getParameter("page"); try { pageIndex = Integer.parseInt(pageStr); } catch (Exception e) { } if (pageIndex < 0) { pageIndex = 0; } if (pageIndex > lastPageIndex) { pageIndex = lastPageIndex; } // ---------------------------------------------------------- StringBuffer sbuffer = new StringBuffer(); exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer); exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,"servlets/image?image="); exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer(pageIndex)); exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, ""); exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, ""); exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, ""); exporter.exportReport(); request.setAttribute("sbuffer", sbuffer);// 将缓冲输出流设置到request中 // 调用自定义生成分页Html的方法,并设置到request中去 request.setAttribute("splitPage", splitPage(pageIndex, lastPageIndex,localUrl, backUrl)); } /** * 统一进行报表处理的函数---导出Excel * * @param request * 从Action传递过来的request * @param response * 从Action传递过来的response * @param parameter * Map类型的参数集合 * @param reportFile * 报表真实路径 * @param output_file_name * 输出文件的文件名,不要包含中文名 * @throws Exception */ public static void procExcelRpt(HttpServletRequest request, HttpServletResponse response, Map parameter, String reportFile, String output_file_name) throws Exception { Connection conn = null; // 获取Connection连接 //GetCon con = new GetCon(); //conn = con.getCon(); SQLConnection dbaccess=SQLConnection.getInstance(); conn = dbaccess.getConnection(); JRXlsExporter exporter = new JRXlsExporter(); // JasperReport的Excel导出对象 // 设置导出Excel不分页 parameter.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE); // 填充报表的路径、参数、数据库连接 JasperPrint report = JasperFillManager.fillReport(reportFile,parameter, conn); conn.close();// 关闭连接 ByteArrayOutputStream xlsOutStream = new ByteArrayOutputStream();// 二进制输出流 // 获取response的的输出流,并转换成缓冲输出流,有效处理数据量过大的情况 BufferedOutputStream bufferOut = new BufferedOutputStream(response.getOutputStream()); // 设置导出对象的各个参数 exporter.setParameter(JRExporterParameter.JASPER_PRINT, report); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsOutStream); exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,Boolean.TRUE); // 删除记录最下面的空行 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,Boolean.FALSE);// 删除多余的ColumnHeader exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND,Boolean.FALSE);// 显示边框 if (!CFormat.isEmpty(output_file_name)) { exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,output_file_name); } exporter.exportReport(); // 从二进制输出流获取字节数组 byte bytes[] = xlsOutStream.toByteArray(); xlsOutStream.close(); // 设置response的格式 response.setContentType("application/vnd.ms-excel"); // response.setContentType("application/x-msdownload"); if (!CFormat.isEmpty(output_file_name)) { response.setHeader("content-disposition", "attachment; filename=\""+ output_file_name + ".xls\""); } response.setContentLength(bytes.length); request.setAttribute("bufferOut", bufferOut);// 将缓冲输出流设置到request中 request.setAttribute("bytes", bytes);// 将字节数组设置到request中 } /** * 统一进行报表处理的函数---导出PDF * * @param request * 从Action传递过来的request * @param response * 从Action传递过来的response * @param parameter * Map类型的参数集合 * @param reportFile * 报表真实路径 * @param output_file_name * 输出文件的文件名,不要包含中文名 * @throws Exception */ public static void procPdfRpt(HttpServletRequest request, HttpServletResponse response, Map parameter, String reportFile, String output_file_name) throws Exception { Connection conn = null; // 获取Connection连接 //GetCon con = new GetCon(); //conn = con.getCon(); SQLConnection dbaccess=SQLConnection.getInstance(); conn = dbaccess.getConnection(); JRPdfExporter exporter = new JRPdfExporter(); // JasperReport的PDF导出对象 ServletOutputStream servletOutputStream = response.getOutputStream(); // 设置导出Excel不分页 parameter.put(JRParameter.IS_IGNORE_PAGINATION, Boolean.TRUE); // 填充报表的路径、参数、数据库连接 JasperPrint report = JasperFillManager.fillReport(reportFile,parameter, conn); conn.close();// 关闭连接 ByteArrayOutputStream xlsOutStream = new ByteArrayOutputStream();// 二进制输出流 // 获取response的的输出流,并转换成缓冲输出流,有效处理数据量过大的情况 BufferedOutputStream bufferOut = new BufferedOutputStream(response.getOutputStream()); // 设置导出对象的各个参数 exporter.setParameter(JRExporterParameter.JASPER_PRINT, report); exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsOutStream); if (!CFormat.isEmpty(output_file_name)) { exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,output_file_name); } exporter.exportReport(); // 从二进制输出流获取字节数组 byte bytes[] = xlsOutStream.toByteArray(); xlsOutStream.close(); // 设置response的格式 response.setContentType("application/pdf"); if (!CFormat.isEmpty(output_file_name)) { response.setHeader("content-disposition", "attachment; filename=\""+ output_file_name + ".pdf\""); } response.setContentLength(bytes.length); request.setAttribute("bufferOut", bufferOut);// 将缓冲输出流设置到request中 request.setAttribute("bytes", bytes);// 将字节数组设置到request中 } }