1. 程式人生 > >https://my.oschina.net/u/3507515/blog/2218592

https://my.oschina.net/u/3507515/blog/2218592

此示例演示用 pageoffice 線上開啟編輯儲存office 辦公文件

需要用到的jar包(http://www.zhuozhengsoft.com/dowm/?v=4)下載試用程式包

pageoffice4.5.0.4.jar

1.WEB.XML

<!-- PageOffice Begin -->
	<servlet>
		<servlet-name>poserver</servlet-name>
		<servlet-class>com.zhuozhengsoft.pageoffice.poserver.Server</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/poserver.zz</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/sealsetup.exe</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/posetup.exe</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/pageoffice.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/jquery.min.js</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserver</servlet-name>
		<url-pattern>/pobstyle.css</url-pattern>
	</servlet-mapping>
	<servlet>
		<servlet-name>adminseal</servlet-name>
		<servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSeal</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>adminseal</servlet-name>
		<url-pattern>/adminseal.zz</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>adminseal</servlet-name>
		<url-pattern>/loginseal.zz</url-pattern>
	</servlet-mapping>
	<servlet-mapping>
		<servlet-name>adminseal</servlet-name>
		<url-pattern>/sealimage.zz</url-pattern>
	</servlet-mapping>
	<mime-mapping>
		<extension>mht</extension>
		<mime-type>message/rfc822</mime-type>
	</mime-mapping>
	<context-param>
		<param-name>adminseal-password</param-name>
		<param-value>111111</param-value>
	</context-param>
	<!-- PageOffice End -->

	<servlet>
		<servlet-name>chapter2</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>/WEB-INF/spring-servlet.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>chapter2</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>

2:spring-servlet.xml

<!-- 開始spring mvc的註解 -->
	<mvc:annotation-driven/>
	
	<!-- 這樣根目錄下面的resource的檔案(.css,.js等)就不會被spring的DispatchServlet進行過濾 -->
	<mvc:resources location="/resources/" mapping="/resources/**"/>
	
	<!-- 配置註解掃描的包路徑 -->
	<context:component-scan base-package="web"></context:component-scan>
	
	<!-- 配置action中返回的檢視配置  -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>

3:JSP頁面

(1):index.jsp

<div style="text-align: center;">
			<b>線上編輯儲存Office文件</b>
			<br>
			<a
				href="javascript:POBrowser.openWindowModeless('open.do?type=word' , 'width=1200px;height=800px;');">線上編輯儲存Word文件</a>
<br>
<a
				href="javascript:POBrowser.openWindowModeless('open.do?type=excel' , 'width=1200px;height=800px;');">線上編輯儲存Word文件</a>

			<br>
		</div>

(2)WordOrExcel.jsp

		<script type="text/javascript">
	function Save() {
		document.getElementById("PageOfficeCtrl1").WebSave();
		window.external.close();//關閉POBrowser視窗
	}
</script>
		<div style="width: 100%; height: 100%;">
			<%
				PageOfficeCtrl poCtrl = (PageOfficeCtrl) request
						.getAttribute("pageoffice");
			%>
			<%=poCtrl.getHtmlCode("PageOfficeCtrl1")%>
		</div>

4:後臺程式碼

開啟文件程式碼

@RequestMapping("open")
	public String openword(HttpServletRequest request,
			HttpServletResponse response) {
		String type = request.getParameter("type");
		PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
		// 設定伺服器頁面
		poCtrl.setServerPage(request.getContextPath() + "/poserver.zz");

		// 設定儲存的action
		poCtrl.setSaveFilePage("savefile.do");

		// 設定開啟的文件
		// 開啟word
		if ("word".equals(type)) {
			poCtrl.webOpen("doc/test.doc", OpenModeType.docAdmin, "張三");
		}
		if ("excel".equals(type)) {
			poCtrl.webOpen("doc/test.xls", OpenModeType.docAdmin, "張三");
		}
		request.setAttribute("pageoffice", poCtrl);
		return "WordOrExcel";
	}

儲存程式碼

@RequestMapping("savefile")
	 public  void  savefile(HttpServletRequest request, HttpServletResponse response){
		FileSaver fs = new FileSaver(request, response);
		fs.saveToFile(request.getSession().getServletContext().getRealPath("doc/") + "/" + fs.getFileName());
		fs.close();
	}

 

 

最後開啟文件的效果

 

 

開啟文件的時候需要註冊,這是一個試用的序列號

IMTG6-BSXJ-JGZ6-3BIWM