Java在线生成word excel 文档 -----Springmvc

阅读更多

此示例演示用 pageoffice 在线打开编辑保存office 办公文档

需要用到的jar包(http://www.zhuozhengsoft.com/dowm/?v=4)下载试用程序包

pageoffice4.5.0.4.jar

1.WEB.XML


	<servlet>
		<servlet-name>poserverservlet-name>
		<servlet-class>com.zhuozhengsoft.pageoffice.poserver.Serverservlet-class>
	servlet>
	<servlet-mapping>
		<servlet-name>poserverservlet-name>
		<url-pattern>/poserver.zzurl-pattern>
	servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserverservlet-name>
		<url-pattern>/sealsetup.exeurl-pattern>
	servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserverservlet-name>
		<url-pattern>/posetup.exeurl-pattern>
	servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserverservlet-name>
		<url-pattern>/pageoffice.jsurl-pattern>
	servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserverservlet-name>
		<url-pattern>/jquery.min.jsurl-pattern>
	servlet-mapping>
	<servlet-mapping>
		<servlet-name>poserverservlet-name>
		<url-pattern>/pobstyle.cssurl-pattern>
	servlet-mapping>
	<servlet>
		<servlet-name>adminsealservlet-name>
		<servlet-class>com.zhuozhengsoft.pageoffice.poserver.AdminSealservlet-class>
	servlet>
	<servlet-mapping>
		<servlet-name>adminsealservlet-name>
		<url-pattern>/adminseal.zzurl-pattern>
	servlet-mapping>
	<servlet-mapping>
		<servlet-name>adminsealservlet-name>
		<url-pattern>/loginseal.zzurl-pattern>
	servlet-mapping>
	<servlet-mapping>
		<servlet-name>adminsealservlet-name>
		<url-pattern>/sealimage.zzurl-pattern>
	servlet-mapping>
	<mime-mapping>
		<extension>mhtextension>
		<mime-type>message/rfc822mime-type>
	mime-mapping>
	<context-param>
		<param-name>adminseal-passwordparam-name>
		<param-value>111111param-value>
	context-param>
	

	<servlet>
		<servlet-name>chapter2servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
		<init-param>
			<param-name>contextConfigLocationparam-name>
			<param-value>/WEB-INF/spring-servlet.xmlparam-value>
		init-param>
		<load-on-startup>1load-on-startup>
	servlet>

	<servlet-mapping>
		<servlet-name>chapter2servlet-name>
		<url-pattern>*.dourl-pattern>
	servlet-mapping>

2:spring-servlet.xml


	<mvc:annotation-driven/>
	
	
	<mvc:resources location="/resources/" mapping="/resources/**"/>
	
	
	<context:component-scan base-package="web">context:component-scan>
	
	
	<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

"text-align: center;"> <b>在线编辑保存Office文档b>
<a href="javascript:POBrowser.openWindowModeless('open.do?type=word' , 'width=1200px;height=800px;');">在线编辑保存Word文档a>
<a href="javascript:POBrowser.openWindowModeless('open.do?type=excel' , 'width=1200px;height=800px;');">在线编辑保存Word文档a>
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();
	}

 

 

最后打开文档的效果

Java在线生成word excel 文档 -----Springmvc_第1张图片

 

Java在线生成word excel 文档 -----Springmvc_第2张图片

 

打开文档的时候需要注册,这是一个试用的序列号

IMTG6-BSXJ-JGZ6-3BIWM

你可能感兴趣的:(excel,spring,mvc,java)