MVC模式下对pageOffice插件的简单整合使用

MVC模式下对pageOffice插件的简单整合使用PageOffice能够在线编辑Word、Excel、PowerPoint文档,Word/Excel模板动态填充,Word/Excel在线输入提交,系统数据导入导出word、excel文档的Office快速开发组件库,是目前把Office应用到Web平台上的最全面、最先进的解决方案。PageOffice为软件开发者构建了一套简洁高效、统一的Office对象接口,同时无缝支持doc、docx、xls、xlsx、ppt、pptx等流行Office文件格式。

最近需要项目用到在线打开word和excel文档处理,在PageOffice官网下载了文档研究了下:官网地址:http://www.zhuozhengsoft.com/

不多说,直接写上学习过程,请大家指正。练习过程整合了Struts进行练习,其他框架可以结合具体业务添加:

在使用之前有几点要注意:

1、在本机上安装posetup.exe,如果用到印章还要安装sealsetup.exe.

2、这个在最新版本的chrome中不能直接打开,通过link方式才行,没弄过,可以看一下。刚开始我就遇到这种问题,IE、firefox可以,还有就是版本问题

3、系统会自动检测本机上有的office软件,自动检测第一次安装的office软件(避免同时有WPS和MSOffice)。

4、pageOffice是收费的,可以试用,为避免在页面中总是输入注册,可离线生成license.lic,copy到lib下,方便开发。

5、。。。。。。。

环境:eclipse3.7+jdk6+tomcat7

第一步:新建一个web工程:

工程目录如下:

MVC模式下对pageOffice插件的简单整合使用

注:doc 目录下问为文档模板或pdf文件等,可自动生成带有印章的公司文档(如请假条,在线填写姓名、事由等,生成文档,再由领导同意,生成一份完整请假条)。

第二步:在web.xml中添加官网给出的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>pageOfficeDemo</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <filter>
  	<filter-name>struts2</filter-name>
  	<filter-class>
  		org.apache.struts2.dispatcher.FilterDispatcher
  	</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>*.action</url-pattern>
  </filter-mapping>
  <filter-mapping>
	<filter-name>struts2</filter-name>
	<url-pattern>*.jsp</url-pattern>
  </filter-mapping>

  
  <!-- 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>/poserver.do</url-pattern> 
  </servlet-mapping> 
  <servlet-mapping> 
	<servlet-name>poserver</servlet-name> 
	<url-pattern>/pageoffice.cab</url-pattern>
  </servlet-mapping>
  <servlet-mapping> 
	<servlet-name>poserver</servlet-name> 
	<url-pattern>/popdf.cab</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>
    <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.do</url-pattern> 
  </servlet-mapping>
  <servlet-mapping> 
    <servlet-name>adminseal</servlet-name> 
    <url-pattern>/loginseal.do</url-pattern>
  </servlet-mapping>
  <servlet-mapping> 
    <servlet-name>adminseal</servlet-name> 
    <url-pattern>/sealimage.do</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>
  
  
  <!-- 连接数据库信息配置-->
  <context-param>
		<param-name>adminseal-charset</param-name>
		<param-value>UTF-8</param-value>
	</context-param>
  
 <context-param>
  	<param-name>posealdb-driver</param-name>
  	<param-value>com.mysql.jdbc.Driver</param-value>
  </context-param>
  <context-param>
  	<param-name>posealdb-url</param-name>
  	<param-value>jdbc:mysql://localhost:3306/pageoffice</param-value>
  </context-param>
  <context-param>
  	<param-name>posealdb-username</param-name>
  	<param-value>root</param-value>
  </context-param>
  <context-param>
  	<param-name>posealdb-password</param-name>
  	<param-value>root</param-value>
  </context-param>
  
  <!-- PageOffice End -->
</web-app>

第三步:进行代码编写

 Struts.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	
	<package name="demo" extends="struts-default">
	
		<action name="createWord" class="com.demo.action.WordDemo" method="create">
			<result name="create">/createWord.jsp</result>
		</action>
		<action name="autoInsert" class="com.demo.action.WordDemo" method="autoInsert">
			<result name="autoInsert">/autoInsert.jsp</result>
		</action>
	
		<action name="openword" class="com.demo.action.WordDemo" method="openword">
			<result name="openword">/editword.jsp</result>
		</action>
		<action name="openPDF" class="com.demo.action.WordDemo" method="openPDF">
			<result name="pdf">/openPDF.jsp</result>
		</action>
		
		<action name="wordToPDF" class="com.demo.action.WordDemo" method="wordToPDF">
			<result name="wordToPDF">/wordToPDF.jsp</result>
		</action>
		
		<action name="saveword" class="com.demo.action.WordDemo" method="saveword">
			<result name="saveword">/savefile.jsp</result>
		</action>
		<action name="openexcel" class="com.demo.action.ExcelDemo" method="openexcel">
			<result name="openexcel">/editexcel.jsp</result>
		</action>
		
		<action name="saveexcel" class="com.demo.action.ExcelDemo" method="saveexcel">
			<result name="saveexcel">/savefile.jsp</result>
		</action>
	</package>

</struts>    

操作word和excel的action(其他的可参看手册练习):

WordDemo:

package com.demo.action;

import java.awt.Color;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zhuozhengsoft.pageoffice.BorderStyleType;
import com.zhuozhengsoft.pageoffice.DocumentOpenType;
import com.zhuozhengsoft.pageoffice.DocumentVersion;
import com.zhuozhengsoft.pageoffice.FileMakerCtrl;
import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PDFCtrl;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.ThemeType;
import com.zhuozhengsoft.pageoffice.wordwriter.WordDocument;

public class WordDemo extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {

	private static final long serialVersionUID = -758686623642845302L;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private String message = "";
	
	private String title;
	

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		System.out.println(title);
		this.title = title;
	}

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getMessage() {
		return message;
	}

	public String create() throws Exception {
		PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(
				ServletActionContext.getRequest());
		System.out.println("create执行到此---------》" + poCtrl1);
		poCtrl1.setServerPage(request.getContextPath() + "/poserver.zz"); // 此行必须

		// 隐藏菜单栏
		poCtrl1.setMenubar(true);
		// 设置Office办公软件类型
		// poCtrl1.setOfficeVendor(OfficeVendorType.WPSOffice);

		poCtrl1.addCustomToolButton("保存", "SaveDocument()", 1);
		// 隐藏工具栏
		poCtrl1.setCustomToolbar(true);
		// 设置边框颜色
		poCtrl1.setBorderColor(Color.blue);
		// 设置边框样式
		poCtrl1.setBorderStyle(BorderStyleType.BorderThin);
		// 设置标题栏文字
		poCtrl1.setCaption("创建一个文档");

		// 保存前的检查
		// poCtrl1.setJsFunction_BeforeDocumentSaved("BeforeDocumentSaved()");

		// 设置保存页面
		poCtrl1.setSaveFilePage("saveword.action");

		// 新建Word文件,webCreateNew方法中的两个参数分别指代“操作人”和“新建Word文档的版本号”
		poCtrl1.webCreateNew("vivid", DocumentVersion.Word2007);
		poCtrl1.setTagId("PageOfficeCtrl1"); // 此行必须

		return "create";
	}

	/**
	 * 在线打开一个PDF
	 */
	public String openPDF() throws Exception {
		PDFCtrl pdf = new PDFCtrl(ServletActionContext.getRequest());
		pdf.setServerPage(request.getContextPath() + "/poserver.zz");
		System.out.println(request.getContextPath());
		// 设置界面样式
		pdf.setBorderColor(Color.RED);
		pdf.setCaption("创建一个PDF");
		pdf.setBorderStyle(BorderStyleType.BorderFlat);
		pdf.setTheme(ThemeType.Office2010);
		//设置菜单选项
		pdf.addCustomToolButton("打印", "Print()", 6);
		pdf.addCustomToolButton("隐藏/显示书签", "SetBookmarks()", 0);
		pdf.addCustomToolButton("-", "", 0);
		//搜索设置
		pdf.addCustomToolButton("搜索", "SearchText()", 0);
		pdf.addCustomToolButton("搜索下一个", "SearchTextNext()", 0);
		pdf.addCustomToolButton("搜索上一个", "SearchTextPrev()", 0);
		pdf.addCustomToolButton("实际大小", "SetPageReal()", 16);
		pdf.addCustomToolButton("适合页面", "SetPageFit()", 17);
		pdf.addCustomToolButton("适合宽度", "SetPageWidth()", 18);
		pdf.addCustomToolButton("-", "", 0);
		pdf.addCustomToolButton("首页", "FirstPage()", 8);
		pdf.addCustomToolButton("上一页", "PreviousPage()", 9);
		pdf.addCustomToolButton("下一页", "NextPage()", 10);
		pdf.addCustomToolButton("尾页", "LastPage()", 11);
		pdf.addCustomToolButton("-", "", 0);
		// // 关闭菜单
		// pdf.setMenubar(false);
		// 打开PDF文档
		pdf.webOpen("doc/test.pdf");
		pdf.setTagId("PDFCtrl1");

		return "pdf";
	}
/**
 * 将文档转为pdf
 */
	public String wordToPDF() throws Exception{
		
		FileMakerCtrl fmCtrl = new FileMakerCtrl(request);
		fmCtrl.setServerPage(request.getContextPath()+"/poserver.zz");
		WordDocument doc = new WordDocument();
		//禁用右击事件
		doc.setDisableWindowRightClick(true);
		//给数据区域赋值,即把数据填充到模板中相应的位置
		doc.openDataRegion("PO_company").setValue("北京卓正志远软件有限公司  ");
		fmCtrl.setSaveFilePage("saveword.action");
		fmCtrl.setWriter(doc);
		fmCtrl.setJsFunction_OnProgressComplete("OnProgressComplete()");
		fmCtrl.fillDocumentAsPDF("doc/template.doc", DocumentOpenType.Word, "a.pdf");
		fmCtrl.setTagId("FileMakerCtrl1"); //此行必须

		return "wordToPDF";
	}
	/**
	 * 
	 * @Title: autoInsert
	 * @Description: 自动添加到文档中
	 * @author vivid
	 * @time 2016-3-24 下午4:38:38
	 * @return
	 * @throws Exception
	 */
	public String autoInsert() throws Exception {
		// 1、初始化实例
		FileMakerCtrl fmCtrl = new FileMakerCtrl(
				ServletActionContext.getRequest());
		fmCtrl.setServerPage(request.getContextPath() + "/poserver.zz");
		System.out.println(request.getContextPath());
		// 生成随机id
		String id = String.valueOf(new Random().nextInt(100));
		System.out.println(id);
		// String id = "2";
		request.setAttribute("id", id);
		System.out.println(title);
		if (id != null && id.length() > 0) {
			// WordDocument 类代表一个Word文档,用来读取用户输入到Word文档中的数据
			WordDocument doc = new WordDocument();
			// 禁用右击事件
			doc.setDisableWindowRightClick(true);
			// 给数据区域赋值,即把数据填充到模板中相应的位置
			doc.openDataRegion("PO_company").setValue(title);
		//	doc.openDataRegion("po_second").setValue("mftcc" + id);
			// 设置 saveMaker.jsp 用来保存文档。
			fmCtrl.setSaveFilePage("saveMaker.jsp?id=" + id);
			fmCtrl.setWriter(doc);
			fmCtrl.setJsFunction_OnProgressComplete("OnProgressComplete()");
			fmCtrl.setFileTitle("文档" + id + ".doc");
			fmCtrl.fillDocument("doc/template.doc", DocumentOpenType.Word);
		}
		fmCtrl.setTagId("FileMakerCtrl1"); // 此行必须

		return "autoInsert";
	}

	/*
	 * 打开文档并修改保存
	 */
	public String openword() throws Exception {
		PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(
				ServletActionContext.getRequest());
		poCtrl1.setServerPage("poserver.zz"); // 此行必须
		// 设置标题
		poCtrl1.setCaption("打开文档操作");
		// 创建工具条
		poCtrl1.addCustomToolButton("保存", "SaveDocument()", 1);
		poCtrl1.addCustomToolButton("-", "", 0);
		poCtrl1.addCustomToolButton("打印", "ShowPrintDlg()", 6);
		poCtrl1.addCustomToolButton("-", "", 0);
		poCtrl1.addCustomToolButton("全屏切换", "SetFullScreen()", 4);
		poCtrl1.addCustomToolButton("-", "", 0);
		poCtrl1.addCustomToolButton("加盖印章", "AddSeal()", 5);
		poCtrl1.addCustomToolButton("手写签批", "AddHandSign()", 5);
		poCtrl1.addCustomToolButton("验证印章", "VerifySeal()", 5);
		poCtrl1.setSaveFilePage("saveword.action");
		poCtrl1.webOpen("doc/test.doc", OpenModeType.docNormalEdit, "张三");
		poCtrl1.setTagId("PageOfficeCtrl1"); // 此行必须
		return "openword";
	}

	/*
	 * 文档保存操作
	 */
	public String saveword() throws Exception {
		FileSaver fs = new FileSaver(request, response);
		request.setAttribute("FileSaver", fs);
		fs.saveToFile(request.getSession().getServletContext()
				.getRealPath("doc/")
				+ "/" + fs.getFileName());
		// fs.saveToFile(request.getContextPath()+"/doc/"+fs.getFileName());
		System.out.println(request.getSession().getServletContext()
				.getRealPath("doc/"));
		System.out.println(fs.getFileName());
		fs.showPage(200, 400);
		message = "保存成功!";
		return "saveword";
	}

}

ExcelDemo:

package com.demo.action;

import java.awt.Color;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.zhuozhengsoft.pageoffice.BorderStyleType;
import com.zhuozhengsoft.pageoffice.FileSaver;
import com.zhuozhengsoft.pageoffice.OpenModeType;
import com.zhuozhengsoft.pageoffice.PageOfficeCtrl;
import com.zhuozhengsoft.pageoffice.excelwriter.Cell;
import com.zhuozhengsoft.pageoffice.excelwriter.Sheet;
import com.zhuozhengsoft.pageoffice.excelwriter.Workbook;

/**
 * 
 * @ClassName: ExcelDemo
 * @Description: TODO
 * @author vivid
 * @date 2016-3-24 下午1:22:55
 * 
 */
@SuppressWarnings("serial")
public class ExcelDemo extends ActionSupport implements ServletRequestAware,
		ServletResponseAware {

	private HttpServletRequest request;
	private HttpServletResponse response;
	private String message = "";

	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getMessage() {
		return message;
	}

	// 此处只简单的实现了Excel文件的在线编辑和保存,
	// 动态填充单元格、提交单元格等其他功能的实现请参考通过JSP编写的示例
	public String openexcel() throws Exception {
		PageOfficeCtrl poCtrl1 = new PageOfficeCtrl(
				ServletActionContext.getRequest());
		poCtrl1.setServerPage("poserver.do"); // 此行必须

		poCtrl1.setCaption("操作Excel文档");
		//设置边框的类型
		poCtrl1.setBorderStyle(BorderStyleType.BorderThin);
		//设置边框的颜色
		poCtrl1.setBorderColor(Color.blue);
		
		// 代表一个Excel文档,用来读取用户输入到Excel表格中的数据,填充Excel表格
		Workbook workbook = new Workbook();
		// 参数为要打开的Excel文件中的Sheet表单的名称
		Sheet sheet = workbook.openSheet("Sheet1");
		// 获取sheet名称
		String name = sheet.getName();
		System.out.println("--------" + name);
		// 打开指定的单元格(“B4”单元格),并返回 Cell 对象
		Cell B4 = sheet.openCell("B4");
		// 给单元格赋值
		B4.setValue("B产品");
		// 设置单元格前景色
		B4.setForeColor(Color.green);
		
		
		// 打开指定的表格,并返回 Table 对象
		// Table XXX =sheet.openTable("XXX");
		

		// 提交单元格数据
		// B4.setSubmitName("pname");

		poCtrl1.setWriter(workbook);
		// 工具栏
		poCtrl1.addCustomToolButton("保存", "SaveDocument()", 1);
		poCtrl1.addCustomToolButton("-", "", 0);
		poCtrl1.addCustomToolButton("打印", "ShowPrintDlg()", 6);
		poCtrl1.addCustomToolButton("-", "", 0);
		poCtrl1.addCustomToolButton("全屏切换", "SetFullScreen()", 4);
		poCtrl1.addCustomToolButton("-", "", 0);
		poCtrl1.addCustomToolButton("加盖印章", "AddSeal()", 5);
		poCtrl1.addCustomToolButton("手写签批", "AddHandSign()", 5);
		poCtrl1.addCustomToolButton("验证印章", "VerifySeal()", 5);
		// 设置保存页
		poCtrl1.setSaveFilePage("saveexcel.action");
		// 打开文件
		poCtrl1.webOpen("doc/test.xls", OpenModeType.xlsNormalEdit, "张三");
		poCtrl1.setTagId("PageOfficeCtrl1"); // 此行必须
		return "openexcel";
	}

	public String saveexcel() throws Exception {
		FileSaver fs = new FileSaver(request, response);
		request.setAttribute("FileSaver", fs);
		// 将修改的Excel文件保存到相应的路径下
		fs.saveToFile(request.getSession().getServletContext()
				.getRealPath("doc/")
				+ "/" + fs.getFileName());
		// 弹出窗口显示保存结果
		// fs.showPage(300, 300);
		// message = "显示自定义保存结果。";
		// 设置保存结果信息
		fs.setCustomSaveResult("success");
		return "saveexcel";
	}

}

下面以在线生成word示例作为记录,其他自己练习,或下载我的代码查看:
http://download.csdn.net/detail/erris/9474348

首页index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
    <title>pageOfficeDemo</title>
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
  </head>
  <script type="text/javascript">
 
  
  </script>
  
  <body>
  <div style="text-align:center;">
      <b>PageOfficeDemo</b><br>
      <br>
    <a href="createWord.action" style="font:blue">1、在线创建一个word文档</font></a><br/><br/>
    <a href="openword.action " style="font:blue"> 2、在线编辑保存Word文档</font></a> <br/><br/>
    <a href="href.jsp" style="font:blue">3、在线替换word文档内容</font></a> <br/><br/>
    <a href="openexcel.action"  style="font:blue">4、在线编辑保存Excel文档</font></a> <br/><br/>
       <a href="openPDF.action" style="font:blue">5、在线打开PDF文档</font></a> <br/><br/>
       <a href="wordToPDF.action" style="font:blue">6、word插入数据后保存为PDF</font></a> <br/><br/>
  </div>

  </body>
</html>

生成word文档:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.pageoffice.cn" prefix="po"%> <!-- 必须 -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

</head>
<body>
	<!--**************   卓正 PageOffice 客户端代码开始    ************************-->
	<script language="javascript" type="text/javascript">
		function SaveDocument() {
			var title = document.getElementById("title").value;
				alert(title);
			if (title != null) {

				document.getElementById("PageOfficeCtrl1").WebSave();
			} else {
				alert("请输入要保存的文件名!!");
			}
			//alert(document.getElementById("PageOfficeCtrl1").CustomSaveResult);
		}
	</script>
	
	<div style="width: 100%; height: 800px">
		<po:PageOfficeCtrl id="PageOfficeCtrl1" />
	</div>
</body>
</html>

运行截图:

MVC模式下对pageOffice插件的简单整合使用

MVC模式下对pageOffice插件的简单整合使用

源码下载地址:http://download.csdn.net/detail/erris/9474348

今天的文章MVC模式下对pageOffice插件的简单整合使用分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/9623.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注