項目經常使用工具類整理(三)--ZSOffice/PageOffice的引用

        在項目中常常會遇到生成一些excel或是word的文檔後用於直接打印這種狀況,尤爲是電子簽章方面,卓正office提供了一個很強大的應用。我就拿excel來生成報表這種類型來介紹一下吧。javascript

一、導入pageoffice.jar,同時也不要忘了poi的相關jar包html

二、下面從前臺的一個請求出發,到顯示出該excel頁面的完整過程,須要註解的地方我會詳細的列出來java

a:經過前天一個url請求(具體的路徑參數我已經給刪除了)jquery

function onClick(event, treeId, treeNode, clickFlag) {
		$('#dialogIframe')[0].src = '${contextPath}zsOffice/view';
	}

b:而後碼出對應的controller層的代碼,返回數據list和pageoffice的顯示模式 ,指向要跳轉的的jsp頁面。web

ModelAndView mav = new ModelAndView("/xxckywh/Excel");
		mav.addObject("List", List);//數據list串
		mav.addObject("mode", mode);//mode會在後文說起,指的是打印仍是下載
		return mav;

c:而後我碼出對應的"/xxckywh/Excel"的jsp代碼,還有他引入的zsExcel.jsp,以及涉及到的"/resources/template/jlqk.xls"excle文件截圖瀏覽器

<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="GB2312"
	import="java.util.List,SOAOfficeX.*"%>
<%request.setCharacterEncoding("gb2312");%>
<jsp:useBean id="user" scope="session" class="com.kkk.domain.User"/>
<%
ZSExcelUtil zsExcelUtil = new ZSExcelUtil();
ExcelResponse SOAExcel = zsExcelUtil.getSOAExcel();
ExcelResSheet sheet;
zsExcelUtil.openSheet("jlqk");//與你打開的excel中的sheet表單名相同
sheet = zsExcelUtil.getSheet();
	
	sheet.useRangeFormat("A5:E5");//循環區間爲A-E
	int line = 5;//從5行開始循環使用
	String sj, dj, sjmc, dw, yj;
	List<Jlqk> jlqkList = (List<Jlqk>)request.getAttribute("List");//處理後臺傳過來的List
	for(Jlqk jlqk : jlqkList){
		sj = SysTools.formatDate(jlqk.getSjsj(), "date");
		if (!Tools.isEmpty(sj)) {
			//sj = sj.substring(0, 4) + "年" + sj.substring(5, 7) + "月";
			sj = sj.substring(0, 4) + sj.substring(5, 7)  ;
		} 
		dj = jlqk.getDj();
		sjmc = jlqk.getSjmc();
		dw = jlqk.getSydw();
		yj = jlqk.getSjsy();
		
		sheet.openCell("A"+line).setValue(sj);
		sheet.openCell("B"+line).setValue(dj);
		sheet.openCell("C"+line).setValue(sjmc);
		sheet.openCell("D"+line).setValue(dw);
		sheet.openCell("E"+line++).setValue(yj);
	}
	sheet.endRangeFormat();//循環結束

	SOAOfficeCtrl SOACtrl = zsExcelUtil.getSOACtrl(pageContext, SOAExcel);
	SOACtrl.ServerURL = "/soaservlet/soaserv.do";
	SOACtrl.webOpen(request.getContextPath() + "/resources/template/jlqk.xls", 0, user.getUsername(), "Excel.Sheet");
%>

<%@include file="zsExcel.jsp" %>

下面zsExcel.jsp,其中規範了excel調用起來後的工具session

<%@ page language="java" contentType="text/html; charset=gb2312" pageEncoding="GB2312"%>
<%request.setCharacterEncoding("gb2312");%>

<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="${pageContext.request.contextPath}/resources/lib/easyui/jquery.min.js"></script>
</head>
<body bgColor="#ffffff" leftMargin="0" topMargin="0" onLoad="soaOpen();window.focus();">
	<script>
		function soaOpen() { 
			try {
				SOAOfficeCtrl.Toolbars = false;
				SOAOfficeCtrl.Menubar = false;	
			
				SOAOfficeCtrl.AppendToolButton(1, "打印", 6);
				SOAOfficeCtrl.AppendToolButton(2, "打印預覽", 7);
				SOAOfficeCtrl.AppendToolButton(3, "頁面設置", 0);
				SOAOfficeCtrl.AppendToolButton(4, "-");
				SOAOfficeCtrl.AppendToolButton(5, "另存到本機", 1);
				SOAOfficeCtrl.AppendToolButton(6, "-");
				//SOAOfficeCtrl.AppendToolButton(7, "全屏/還原", 4);
// 				SOAOfficeCtrl.ServerURL = "/soaservlet/soaserv.do";
// 				SOAOfficeCtrl.WebOpen("OpenByServerSide");
				return true;
			} catch (e) { 
				var errorStr = e.description;
				if (errorStr.indexOf("爲空或不是對象") > 0) {
					alert("本機還沒有安裝SOAOffice 客戶端控件,請安裝瀏覽器上方黃色提示條或彈出提示框中的SOAOffice 客戶端控件。");
				} else {
					alert(erroStr);
				}
				return false; 
			} 
		}
		
		function handelRequest() {
			<%
				String mode = (String)request.getAttribute("mode");
			%>
			var mode = '<%=mode%>';
			//alert('mode:' + mode);
			
			if (mode == 'print') {
				SOAOfficeCtrl.PrintOut();
			} else if (mode == 'download') {
				//parent.$('#downloadFrame')[0].src = '${pageContext.request.contextPath}/zsOffice/downloadDoc';
				SOAOfficeCtrl.ShowDialog(3);
			}
		}
	</script>
	<script language="Javascript" event="OnInit()" for="SOAOfficeCtrl">
		// 控件打開文檔前觸發,用來初始化界面樣式
	</script>
	<script language="Javascript" event="OnDocumentOpened(str, obj)" for="SOAOfficeCtrl">
		// 控件打開文檔後當即觸發,添加自定義菜單,自定義工具欄,禁止打印,禁止另存,禁止保存等等
		SOAOfficeCtrl.Toolbars = false;
		SOAOfficeCtrl.Menubar = false;	
		SOAOfficeCtrl.AppendToolButton(1, "打印", 6);
		SOAOfficeCtrl.AppendToolButton(2, "打印預覽", 7);
		SOAOfficeCtrl.AppendToolButton(3, "頁面設置", 0);
		SOAOfficeCtrl.AppendToolButton(4, "-");
		SOAOfficeCtrl.AppendToolButton(5, "另存到本機", 1);
		SOAOfficeCtrl.AppendToolButton(6, "-");
		SOAOfficeCtrl.AppendToolButton(7, "全屏/還原", 4);
		
		handelRequest();
	</script>
	<script language="Javascript" event="OnDocumentClosed()" for="SOAOfficeCtrl">
	</script>
	<script language="Javascript" event="OnUserMenuClick(index, caption)" for="SOAOfficeCtrl">
		// 添加自定義菜單項事件響應
	</script>
	<script language="Javascript" event="OnCustomToolBarClick(index, caption)" for="SOAOfficeCtrl">
		// 添加自定義工具欄按鈕事件響應
		if(index == 1) ShowDialog(4);
		if(index == 2) PrintPreview();
		if(index == 3) ShowDialog(5);
		if(index == 5) ShowDialog(2);
		if(index == 7) FullScreen = !FullScreen;
	</script>
	<table cellSpacing="0" cellPadding="0" width="100%" border="0">
		<tbody> 
			<tr valign="top" align="center">
				<td height="685">
					<span id="WEBWORD_object">
					<!-- 引用控件的代碼 -->
					<object id="SOAOfficeCtrl" codeBase="../../lib/ZSOffice.ocx#version=2,0,0,1" height="100%" width="100%" classid="clsid:AD06827C-D92F-4648-B880-138AF11E8A13" VIEWASTEXT>
						<%--<param name="MainStyle" value="2">
						<param name="BorderStyle" value="0">--%>
						<span STYLE="color:red">本機還沒有安裝卓正OFFICE組件,請安裝瀏覽器上方黃色提示條或彈出提示框中的卓正OFFICE組件。</span>
					</object>
					</span>
				</td>
			</tr>
		</tbody>
	</table>
</body>
</html>

excel截圖,注意我標紅位置和代碼中的對應位置dom

在jsp中須要導入的excel工具類ZSExcelUtil.javajsp

import javax.servlet.jsp.PageContext;

import SOAOfficeX.*;

public class ZSExcelUtil {
    private ExcelResponse SOAExcel;
    private ExcelResSheet sheet;
    private SOAOfficeCtrl SOACtrl;
    
    public ZSExcelUtil() {
    	try {
	    	SOAExcel = new ExcelResponse();
	    	SOAExcel.DisableSheetDoubleClick = true;
	    	SOAExcel.DisableSheetRightClick = true;
	    	SOAExcel.DisableSheetSelection = true;
    	} catch (Exception e) {
    		e.printStackTrace();
    	}
    }

    public ExcelResSheet getSheet() {
        return sheet;
    }

    public void setSheet(ExcelResSheet sheet) {
        this.sheet = sheet;
    }

    public ExcelResponse getSOAExcel() {
        return SOAExcel;
    }

    public void setSOAExcel(ExcelResponse excel) {
        SOAExcel = excel;
    }

    public SOAOfficeCtrl getSOACtrl(PageContext pageContext,ExcelResponse SOAExcel) {
		try {
		    SOACtrl = new SOAOfficeCtrl(pageContext);
		    SOACtrl.Server = pageContext.getRequest().getServerName();
		    SOACtrl.ServerPort = "4357";	//服務端口
		    SOACtrl.MainStyle = 2;
		    SOACtrl.Toolbars = false; 
		    SOACtrl.Menubar = false;
		    SOACtrl.Caption = "";
		    SOACtrl.Titlebar = false;	//隱藏、顯示標題欄
		    SOACtrl.assign(SOAExcel); 
		} catch(Exception e) {
		    e.printStackTrace();	
		}
        return SOACtrl;
    }

    public void setSOACtrl(SOAOfficeCtrl ctrl) {
        SOACtrl = ctrl;
    }
    
    public void openSheet(String sheetName) {
    	try {
			sheet = SOAExcel.openSheet(sheetName);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	sheet.setFormMode(true);
    }
}

 

這篇日誌寫的比較籠統,應該會有疏漏的地方,若是你做爲教程來看的話,應該有不少地方須要你本身去探索,你也能夠聯繫我。ide

相關文章
相關標籤/搜索