weboffice功能的实现:在线编辑,保护文档部分编辑,显示,下载

点聚Weboffice IE 上的用法:

Weboffice7文件夹是weboffice的文件:

.cab是一个压缩形式的文件,里面包含了安装信息,IE会自动安装。

Main.js是weboffice的方法接口集合。其他的是我自己写的js方法;

 

从页面开始展示代码:

HTML:

EditContract.aspx;(无后台代码)

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="EditContract.aspx.cs" Inherits="WebApplication1.EditContract" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>在线编辑</title>
    <script src="/tmp/weboffice7/main.js" type="text/javascript"></script>
    <script src="/tmp/jquery-1.8.2.min.js" type="text/javascript"></script>
    <script src="/tmp/EditContract.js" type="text/javascript"></script>
   
   </head>

<body>
    <form id="form1" runat="server">
    <div  >
        <input type="button" value="全屏" onclick="showFullScreen()" id="btnFullScreen" />
        <input type="button" value="保存" onclick="SaveFile()" id="btnSave" />
        <input type="button" value="保护文档" onclick="ProtectFull()" id="ProtectFull" />
         <input type ="hidden" value="<%=filename %>" id = "url" />
    </div>
    <div>
        <SCRIPT src="/tmp/weboffice7/LoadWebOffice.js"></SCRIPT>
    </div>

    </form>

</body>

Js代码:

 

LoadWebOffice.js:

var s = ""
s += "<object id=WebOffice1 height=768 width='100%' style='LEFT: 0px; TOP: 0px;z-index:-99;'  classid='clsid:E77E049B-23FC-4DB8-B756-60529A35FAD5' codebase='/tmp/weboffice7/WebOffice.cab#Version=7,0,1,0'>"
s +="<param name='_ExtentX' value='6350'><param name='_ExtentY' value='6350'>"
s +="</OBJECT>"
document.write(s)

EditContract.js:

//==========================公共变量====================
var webObj;         //weboffic对象
var docSaveUrl;     //网络路径
var filePath = "/Upload/"; //文件夹
var doctype = ".doc";//文件类型
var contract_name; //word文件名称

//==========================初始化======================
$(document).ready(function () {
    webObj = document.getElementById("WebOffice1");
    docSaveUrl = document.URL;
    contract_name = '设备买保技术协议标准';
    LoadFile(); //加载显示word文件
    hide_toolBar(); //隐藏工具栏
    ProtectFull(); //设置文档保护 (word在线编辑含有保护文档的word防止部分可编辑部分不可编辑失效执行的)
});
//加载显示word文件
function LoadFile() {
    try {
        var savePath = docSaveUrl.substring(0, docSaveUrl.lastIndexOf("/"));
        //判断文件是否存在
        if (isExistFile(savePath + filePath + contract_idname + ".docx")) {
            webObj.LoadOriginalFile(savePath + filePath + contract_idname + ".docx", doctype);
        } else {
        alert('未找到该文件!');
        }
    } catch (e) {
        alert("异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
    }
}

//判断文件是否存在
function isExistFile(url) {
    var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest(); //其他浏览器    
    }
    else if (window.ActiveXObject) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); //旧版IE    
        }
        catch (e) { }
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); //新版IE    
        }
        catch (e) { }
        if (!xmlhttp) {
            window.alert("不能创建XMLHttpRequest对象");
        }
    }
    yourFileURL = url;
    xmlhttp.open("GET", yourFileURL, false);
    xmlhttp.send();
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.status == 200)
            return true; //url存在     
        else
        //url不存在
            return false;
    }
}
//全屏方法
function showFullScreen() {
    try {
        webObj.FullScreen = true;
    } catch (e) {
        alert("异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
    }
}
//隐藏菜单
function notMenu() {
    try {
        webObj.HideMenuAction(1, 0x100000 + 0x200000 + 0x400000 + 0x800000 + 0x1000000 + 0x2000000 + 0x4000000 + 0x8000000 + 0x10000000);
        webObj.HideMenuAction(5, 0); //激活设置
    } catch (e) {
        alert("异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
    }
}
/****************************************************
*			
*				设置文档保护 
*
****************************************************/
function ProtectFull() {
    try {
        document.all.WebOffice1.ProtectDoc(1, 1, "abc123!!");
    } catch (e) {
        alert("异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
    }
}

/****************************************************
*			
*	---隐藏office菜单或功能区
*
/*****************************************************/

function hideAll() {
    document.all.WebOffice1.HideMenuArea('hideall', '', '', '');
}

//隐藏工具栏
function hide_toolBar() {
    try {
        document.all.WebOffice1.HideMenuArea('hideall', '', '', '');
        document.all.WebOffice1.ShowToolBar = 0
        notMenu();
    } catch (e) {

    }
}
//上面的方法实现了在线显示编辑
//保存的话下面参考
//保存word,同时保存服务器,FTP,数据库(后台实现)
function SaveFile() {
    try {
        if (webObj.IsSaved() != 0) { //0已被修改,非0未被修改
            alert("没有内容被修改!");
            return;
        }
        var savePath = docSaveUrl.substring(0, docSaveUrl.lastIndexOf("/"));
        var returnValue;  // 保存页面的返回值 
        webObj.HttpInit(); // 初始化Http引擎 
        //------------------------传递到后台的值--参数自己设置
        webObj.HttpAddPostString("DocName", file_name);  // 添加相应的Post元素
        webObj.HttpAddPostString("Contract_ID", contract_id);
        webObj.HttpAddPostString("user_id", user_id);
        webObj.HttpAddPostString("user_name", user_name);
        webObj.HttpAddPostString("contract_name", escape(contract_name));
        //-------------------------escape(contract_name)防止传递汉字在后台获取乱码问题
        webObj.HttpAddPostCurrFile("DocContent", "");   
        returnValue = webObj.HttpPost(savePath + "/Upload.aspx"); //提交并返回
        if (returnValue != "" || returnValue != null) {
            ProtectFull();
            alert("保存成功");
        }
        else {
            alert("保存失败")
        }

    } catch (e) {
        alert("异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
    }
}

//======================== 方法 END==========================

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.Common;
using System.Data;
using System.Configuration;


namespace WebApplication1.UI.EditContract
{
    public partial class Upload : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string url = "http://" + Request.ServerVariables["HTTP_HOST"].ToString() + Request.ServerVariables["PATH_INFO"].ToString();  //获得URL的值
                int i = url.LastIndexOf("/");
                url = url.Substring(0, i);
                Response.Clear();
                //获取参数的方法
                string ID = Request.Params["Contract_ID"];
                string DocTitle = HttpUtility.UrlDecode(Request.Params["DocName"]);
                string user_id = Request.Params["user_id"];
                string user_name = Request.Params["user_name"];
                string contract_name = HttpUtility.UrlDecode(Request.Params["contract_name"]);
                FileEntity docmodel = new FileEntity();
                HttpPostedFile upPhoto = Request.Files[0];
                string fileName = Path.GetFileName(upPhoto.FileName);
                //保存为时间戳的文件名
                //string fname = DateTime.Now.ToString("yyyyyMMddHHmmss") + "." + "docx";
                //保存自己的传过来的文件名
                string fname = contract_name + "." + "docx";
                if (fileName != null)
                {
                    string uurl = MapPath("Uploadsave/") + fname;
                    string uurlread = MapPath("Uploadread/") + fname;
                    //将路径存数据库的字段中
                    //string sqlInserted = @"sql语句";
                    //int S = gateway.FromCustomSql(sqlInserted)

                    //实例化Ftp方法这里保存了两份文件一份只读一份可写
                    //AgreementTools at = new AgreementTools();
                    //保存上传可写文档
                    upPhoto.SaveAs(uurl);
                    //at.FTP_UPFILE(uurl, fname, "notread");
                    //保存上传只读文档
                    upPhoto.SaveAs(uurlread);
                    //设置文件为只读
                    if (File.Exists(uurlread))
                    {
                        File.SetAttributes(uurlread, FileAttributes.ReadOnly);
                    }
                    //at.FTP_UPFILE(uurlread, fname, "read");

                    Response.Write(uurl);
                }

            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }

            Response.End();

        }

    }
}

ps:保护文档的部分编辑的文档和可编辑的文档显示为不可编辑,文件要为只读

/****************************************************
*			
*				设置文档保护 
*
****************************************************/
function ProtectFull() {
    try {
        //alert(1111111);
        document.all.WebOffice1.ProtectDoc(1, 2, "abc123!!");
    } catch (e) {
        alert("异常\r\nError:" + e + "\r\nError Code:" + e.number + "\r\nError Des:" + e.description);
    }
}

ProtectDoc方法第二个参数改为2

ProtectDoc方法参数可看开发文档;

猜你喜欢

转载自blog.csdn.net/hwc_dd/article/details/82216917