当前位置: 代码迷 >> ASP >> Struts2中兑现JasperReport打印通用模块设计(兼容多种浏览器)
  详细解决方案

Struts2中兑现JasperReport打印通用模块设计(兼容多种浏览器)

热度:428   发布时间:2012-08-30 09:55:54.0
Struts2中实现JasperReport打印通用模块设计(兼容多种浏览器)

/**
?* 报表Applet打印和pdf、xls导出
?*
?* @author liulin
?* @date 2010-3
?* **/
abstract public class ReportAction extends BaseAction {
??? private static String basepath = "/WEB-INF/report/";// 报表存放地址
??? private static String jrepath = "../applet/jre-6u21-windows-i586.exe";//jre-6u19-windows-i586.exe";// jre下载本地路径#Version=6,0,1,9
??? public String reportname;// 无后缀的jrxml报表模板名字
??? public String exportname;// 导出pdf、xls的文件名字
??? public List reportList;// 数据明细集合
??? public Map reportParams;// 报表参数

??? /** 报表填充数据 **/
??? abstract public List getReportList();

??? public void setReportList(List reportList) {
??? ??? this.reportList = reportList;
??? }

??? public Map getReportParams() {
??? ??? return reportParams;
??? }

??? public void setReportParams(Map reportParams) {
??? ??? this.reportParams = reportParams;
??? }

??? public String getReportname() {
??? ??? return reportname;
??? }

??? public void setReportname(String reportname) {
??? ??? this.reportname = reportname;
??? }

??? public String getExportname() {
??? ??? return exportname;
??? }

??? public void setExportname(String exportname) {
??? ??? this.exportname = exportname;
??? }

??? public String getBasepath() {
??? ??? return basepath;
??? }

??? public void setBasepath(String basepath) {
??? ??? this.basepath = basepath;
??? }

??? public String getJrepath() {
??? ??? return jrepath;
??? }

??? public void setJrepath(String jrepath) {
??? ??? this.jrepath = jrepath;
??? }

??? /** applet打印预览,兼容ie、火狐等大多数浏览器 **/
??? public String print() {
??? ??? Enumeration en = this.getRequest().getParameterNames();
??? ??? boolean isIE = isIE();
??? ??? String appleturl;
??? ??? String param = "";
??? ??? for (; en.hasMoreElements();) {
??? ??? ??? String k = (String) en.nextElement();
??? ??? ??? String v = this.getRequest().getParameter(k);
??? ??? ??? if (v != null)
??? ??? ??? ??? param += k + "=" + v.trim() + "&";
??? ??? ??? else
??? ??? ??? ??? param += k + "=&";
??? ??? }
??? ??? if(param.lastIndexOf("&")==param.length()-1)
??? ??? ??? param=param.substring(0,param.length()-1);
??? ??? appleturl = this.getRequest().getRequestURI().replace("_print.action",
??? ??? ??? ??? "_APPLET.action")
??? ??? ??? ??? + "?" + param;

??? ??? String html = "<html><head><title></title>\n"
??? ??? ??? ??? + "<style type=\"text/css\">body {margin:0px 2px;padding:0px;}</style>\n"
??? ??? ??? ??? + "</head><body>\n";
??? ??? html+="<!--[if !IE]> Firefox and others will use outer object -->" +
??? ??? ??? ??? "<object classid=\"java:EmbeddedViewerApplet\" type=\"application/x-java-applet\" WIDTH=\"100%\" HEIGHT=\"100%\" >" +
??? ??? ??? ??? "<PARAM NAME=\"REPORT_URL\" " + " VALUE=\"" + appleturl + "\">" +
??? ??? ??? ??? "<PARAM NAME=\"CODEBASE\" VALUE=\"../applet\">" +
??? ??? ??? ??? "<param name=\"archive\" value=\"jasperreports-applet-3.7.1.jar,commons-logging.jar,commons-collections.jar\" />" +
??? ??? ??? ??? "<!--<![endif]-->" +
??? ??? ??? ??? "<!-- MSIE (Microsoft Internet Explorer) will use inner object -->" +
??? ??? ??? ??? "<object classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\"" +
??? ??? ??? ??? " codebase=\""+jrepath+"\" WIDTH=\"100%\" HEIGHT=\"100%\" >" +
??? ??? ??? ??? "<PARAM NAME=\"CODEBASE\" VALUE=\"../applet\">" +
??? ??? ??? ??? "<param name=\"code\" value=\"EmbeddedViewerApplet.class\" />" +
??? ??? ??? ??? "<param name=\"archive\" value=\"jasperreports-applet-3.7.1.jar,commons-logging.jar,commons-collections.jar\" />" +
??? ??? ??? ??? "<PARAM NAME=\"REPORT_URL\" " + " VALUE=\"" + appleturl + "\">" +
??? ??? ??? ??? "<strong>这个浏览器没有安装 Java Plug-in.<br />" +
??? ??? ??? ??? "<a href=\"http://www.oracle.com/technetwork/java/javase/downloads/index.html\">从最官方下载最新 Java Plug-in.</a></strong><br/>" +
??? ??? ??? ??? "<a href=\""+jrepath+"\">从本地下载?? Java Plug-in</a><br/>" +
??? ??? ??? ??? "<a href=\"../applet/JavaRuntimeEnvironment.reg\">如果安装插件失败,手动修改注册列表来实现(注意:修改JavaRuntimeEnvironment.reg文件中的C:\\\\Program Files\\\\Java\\\\jre6\\\\为你系统jre安装目录;修改1.6.0_21为你当前安装版本)</a>" +
??? ??? ??? ??? "</object>" +
??? ??? ??? ??? "<!--[if !IE]> close outer object -->" +
??? ??? ??? ??? "</object>" +
??? ??? ??? ??? "<!--<![endif]-->";
??? ??? html += "</body></html>";
??? ??? this.renderHtml(html);
??? ??? return NONE;
??? }

??? /** 是否IE **/
??? public boolean isIE() {
??? ??? HttpServletRequest request = this.getRequest();
??? ??? String agent = request.getHeader("USER-AGENT");
??? ??? boolean isIE = false;
??? ??? if (null != agent && -1 != agent.indexOf("MSIE"))
??? ??? ??? isIE = true;
??? ??? return isIE;
??? }

??? /**
??? ?* 根据浏览器类型导出文件中文名称,支持浏览器IE、Firefox、Safari
??? ?*
??? ?* @throws UnsupportedEncodingException
??? ?**/
??? public String getFileNameCode(String fileName)
??? ??? ??? throws UnsupportedEncodingException {
??? ??? HttpServletRequest request = this.getRequest();
??? ??? String agent = request.getHeader("USER-AGENT");
??? ??? if (null != agent && -1 != agent.indexOf("MSIE")) {
??? ??? ??? fileName = java.net.URLEncoder.encode(fileName, "UTF-8").replace(
??? ??? ??? ??? ??? "+", " ");
??? ??? } else if (null != agent && -1 != agent.indexOf("Firefox")) {
??? ??? ??? fileName = new String(fileName.getBytes("UTF-8"), "iso8859-1");
??? ??? } else if (null != agent && -1 != agent.indexOf("Safari")) {
??? ??? ??? fileName = java.net.URLEncoder.encode(fileName, "UTF-8").replace(
??? ??? ??? ??? ??? "+", " ");
??? ??? } else {
??? ??? }
??? ??? return fileName;
??? }

??? /** 导出xls文件 **/
??? public String XLS() throws Exception {
??? ??? HttpServletResponse response = this.getResponse();
??? ??? String sourceFile = ServletActionContext.getServletContext()
??? ??? ??? ??? .getRealPath(basepath + reportname + ".jrxml");
??? ??? File parent = new File(sourceFile).getParentFile();
??? ??? JRXlsExporter exporter = new JRXlsExporter();
??? ??? JasperPrint jasperPrint = null;
??? ??? try {
??? ??? ??? JasperCompileManager.compileReportToFile(sourceFile, new File(
??? ??? ??? ??? ??? parent, reportname + ".jasper").getAbsolutePath());
??? ??? ??? JasperReport jasperReport = (JasperReport) JRLoader
??? ??? ??? ??? ??? .loadObject(sourceFile.replace(".jrxml", ".jasper"));
??? ??? ??? List list = this.getReportList();
??? ??? ??? jasperPrint = JasperFillManager.fillReport(jasperReport,
??? ??? ??? ??? ??? this.reportParams, new JRBeanCollectionDataSource(list));
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? if (null != jasperPrint) {
??? ??? ??? byte[] bytes;
??? ??? ??? ByteArrayOutputStream bo = new ByteArrayOutputStream();
??? ??? ??? exporter
??? ??? ??? ??? ??? .setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
??? ??? ??? exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bo);
??? ??? ??? exporter.exportReport();
??? ??? ??? bytes = bo.toByteArray();
??? ??? ??? if (bytes != null && bytes.length > 0) {
??? ??? ??? ??? response.reset();
??? ??? ??? ??? response.setContentType("application/xls");
??? ??? ??? ??? exportname = exportname + "__"
??? ??? ??? ??? ??? ??? + this.getCurrentTime(new Date(), "yyyy年MM月dd日")
??? ??? ??? ??? ??? ??? + ".xls";
??? ??? ??? ??? response.addHeader("Content-Disposition",
??? ??? ??? ??? ??? ??? "attachment; filename=\"" + getFileNameCode(exportname)
??? ??? ??? ??? ??? ??? ??? ??? + "\"");
??? ??? ??? ??? response.setContentLength(bytes.length);
??? ??? ??? ??? ServletOutputStream sos = response.getOutputStream();
??? ??? ??? ??? sos.write(bytes, 0, bytes.length);
??? ??? ??? ??? sos.flush();
??? ??? ??? ??? sos.close();
??? ??? ??? }
??? ??? }
??? ??? return NONE;
??? }

??? /** 导出pdf文件 **/
??? public String PDF() throws Exception {

??? ??? HttpServletResponse response = this.getResponse();

??? ??? String sourceFile = ServletActionContext.getServletContext()
??? ??? ??? ??? .getRealPath(basepath + reportname + ".jrxml");
??? ??? File parent = new File(sourceFile).getParentFile();
??? ??? JRPdfExporter exporter = new JRPdfExporter();
??? ??? JasperPrint jasperPrint = null;
??? ??? try {
??? ??? ??? JasperCompileManager.compileReportToFile(sourceFile, new File(
??? ??? ??? ??? ??? parent, reportname + ".jasper").getAbsolutePath());
??? ??? ??? JasperReport jasperReport = (JasperReport) JRLoader
??? ??? ??? ??? ??? .loadObject(sourceFile.replace(".jrxml", ".jasper"));
??? ??? ??? List list = this.getReportList();
??? ??? ??? jasperPrint = JasperFillManager.fillReport(jasperReport,
??? ??? ??? ??? ??? this.reportParams, new JRBeanCollectionDataSource(list));
??? ??? } catch (Exception e) {
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? if (null != jasperPrint) {
??? ??? ??? byte[] bytes;

??? ??? ??? ByteArrayOutputStream bo = new ByteArrayOutputStream();
??? ??? ??? exporter
??? ??? ??? ??? ??? .setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
??? ??? ??? exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, bo);
??? ??? ??? exporter.exportReport();
??? ??? ??? bytes = bo.toByteArray();
??? ??? ??? if (bytes != null && bytes.length > 0) {
??? ??? ??? ??? response.reset();
??? ??? ??? ??? response.setContentType("application/pdf");
??? ??? ??? ??? exportname = exportname + "__"
??? ??? ??? ??? ??? ??? + this.getCurrentTime(new Date(), "yyyy年MM月dd日")
??? ??? ??? ??? ??? ??? + ".pdf";
??? ??? ??? ??? response.addHeader("Content-Disposition",
??? ??? ??? ??? ??? ??? "attachment; filename=\"" + getFileNameCode(exportname)
??? ??? ??? ??? ??? ??? ??? ??? + "\"");
??? ??? ??? ??? response.setContentLength(bytes.length);
??? ??? ??? ??? ServletOutputStream sos = response.getOutputStream();
??? ??? ??? ??? sos.write(bytes, 0, bytes.length);
??? ??? ??? ??? sos.flush();
??? ??? ??? ??? sos.close();
??? ??? ??? }
??? ??? }
??? ??? return NONE;
??? }

??? /**
??? ?* applet输出
??? ?*
??? ?* **/
??? public String APPLET() {
??? ??? File reportFile = new File(ServletActionContext.getRequest()
??? ??? ??? ??? .getRealPath(basepath + reportname + ".jasper"));
??? ??? String sourceFile = ServletActionContext.getServletContext()
??? ??? ??? ??? .getRealPath(basepath + reportname + ".jrxml");
??? ??? File parent = new File(sourceFile).getParentFile();
??? ??? JasperPrint jasperPrint = null;
??? ??? try {
??? ??? ??? JasperCompileManager.compileReportToFile(sourceFile, new File(
??? ??? ??? ??? ??? parent, reportname + ".jasper").getAbsolutePath());
??? ??? ??? JasperReport jasperReport = (JasperReport) JRLoader
??? ??? ??? ??? ??? .loadObject(reportFile);
??? ??? ??? List list = this.getReportList();
??? ??? ??? jasperPrint = JasperFillManager.fillReport(jasperReport,
??? ??? ??? ??? ??? this.reportParams, new JRBeanCollectionDataSource(list));
??? ??? } catch (Exception e) {
??? ??? ??? System.out.println("打印出错了:"+e.getMessage());
??? ??? ??? e.printStackTrace();
??? ??? }
??? ??? if (null != jasperPrint) {
??? ??? ??? HttpServletResponse response = this.getResponse();// ServletActionContext.getResponse();
??? ??? ???
??? ??? ??? response.setContentType("application/octet-stream");
??? ??? ??? ServletOutputStream ouputStream;
??? ??? ??? try {
??? ??? ??? ??? ouputStream = response.getOutputStream();
??? ??? ??? ??? ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
??? ??? ??? ??? oos.writeObject(jasperPrint);
??? ??? ??? ??? oos.flush();
??? ??? ??? ??? oos.close();
??? ??? ??? ??? ouputStream.flush();
??? ??? ??? ??? ouputStream.close();
??? ??? ??? } catch (IOException e) {
??? ??? ??? ??? e.printStackTrace();
??? ??? ??? }
??? ??? }
??? ??? return NONE;
??? }

}

  相关解决方案