日期:2014-05-17  浏览次数:20906 次

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\" />" +