首页 > 代码库 > NET中weboffice组件在线编辑文档并保存到服务器上

NET中weboffice组件在线编辑文档并保存到服务器上

页面中组件的引用以及控件触发事件:

   <table width="100%" border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td width="9" background="images/tab_12.gif">
                         <asp:Button ID="btnuploadsave" runat="server" OnClientClick="return SaveDoc()" Text="保存"
                                Style="width: 70px; height: 24px;
                                border: 1px solid #c2e1ef; margin-top: 5px;" />
                        </td>
                        <td bgcolor="e5f1d6">
                            <table width="99%"   border="0" align="center"  cellpadding="0" cellspacing="1" bgcolor="#CECECE">
                                <tr>
                                    <td width="1360px" height="800px">
                                        <script src="js/LoadWebOffice.js" type="text/javascript"></script>
                                    </td>
                                </tr>
                            </table>
                        </td>
                        <td width="9" background="images/tab_16.gif">
                            &nbsp;
                        </td>
                    </tr>

页面中js方法:

// -----------------------------== 保存文档 ==------------------------------------ //
function SaveDoc() {
    var ID = document.getElementById("hfdid").value;
    var DocType = document.getElementById("ddltype").value;
    var title = document.getElementById("txttile").value;
     if(document.getElementById("txttile").value http://www.mamicode.com/==""){
        alert("标题不可为空")
        document.getElementById("txttile").focus();
        return false;
}
    //恢复被屏蔽的菜单项和快捷键
    document.all.WebOffice1.SetToolBarButton2("Standard",1,3);
    document.all.WebOffice1.SetToolBarButton2("Standard",2,3);
    document.all.WebOffice1.SetToolBarButton2("Standard",3,3);
    document.all.WebOffice1.SetToolBarButton2("Standard",6,3);
    if (DocType == "doc") {
        //恢复文件菜单项
        document.all.WebOffice1.SetToolBarButton2("Menu Bar",1,4);
        //恢复 保存快捷键(Ctrl+S) 
        document.all.WebOffice1.SetKeyCtrl(595,0,0);
        //恢复 打印快捷键(Ctrl+P) 
        document.all.WebOffice1.SetKeyCtrl(592,0,0);
    }
    else if(DocType   == "xls") {
        //恢复文件菜单项
        document.all.WebOffice1.SetToolBarButton2("Worksheet Menu Bar",1,4);         
    } 
//初始化Http引擎
    document.all.WebOffice1.HttpInit();            
//添加相应的Post元素
    
    if(ID != ""){          

    document.all.WebOffice1.SetTrackRevisions(0);
    document.all.WebOffice1.ShowRevisions(0);
    document.all.WebOffice1.HttpAddPostString("DID", ID);

     }

document.all.WebOffice1.HttpAddPostString("DocTitle", encodeURI(title));
document.all.WebOffice1.HttpAddPostString("DocType", encodeURI(DocType));
    //把当前文档添加到Post元素列表中,文件的标识符?DocContent
    document.all.WebOffice1.HttpAddPostCurrFile("DocContent","");    
    var vtRet = document.all.WebOffice1.HttpPost(document.getElementById("hfurl").value + "/Upload.aspx");
    if ("succeed" == vtRet) {
        alert("文件上传成功");    
    }else{
        alert("文件上传失败");
}
return return_onclick(); 
}

// -----------------------------== 返回首页 ==------------------------------------ //
function return_onclick() {
document.all.WebOffice1.Close();
window.location.href = "http://www.mamicode.com/Default.aspx";
return false;

}

 

Upload.aspx上传文档处理页面为空白页面,后台代码如下:

protected void Page_Load(object sender, EventArgs e)
        {
            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();
            //ID为文档的主键,如果ID不为空,则更新数据,否则新建一条记录
            string ID = Request.Params["DID"],
                   DocTitle = HttpUtility.UrlDecode(Request.Params["DocTitle"]),
                   DocType = HttpUtility.UrlDecode(Request.Params["DocType"]);

            if (DocType == "")
                DocType = "doc";


            FileEntity docmodel = new FileEntity();
            if (ID != "" && ID != null)
            {
                docmodel = DocDAL.GetFObj(ID);
            }

            DocType = DocType.Substring(0, 3);
            if (Request.Files.Count > 0)
            {
                HttpPostedFile upPhoto = Request.Files[0];
                string fileName = Path.GetFileName(upPhoto.FileName);
                string fname = DateTime.Now.ToString("yyyyyMMddHHmmss") + "." + DocType;
                if (fileName != null)
                {
                    string uurl = MapPath("doc/") + fname;
                    upPhoto.SaveAs(uurl);
                }

             
                docmodel.Did = Convert.ToInt32(ID);
                docmodel.Docurl = url + ("/doc/") + fname;
                docmodel.Doctitle = DocTitle;
                docmodel.Doctype = DocType;
                docmodel.Docdate = DateTime.Now;
                docmodel.Docstate = "1";

                int add = DocDAL.AddFile(docmodel);
                if (add > 0)
                {
                    Response.Write("succeed");
                }
                else
                {
                    Response.Write("failed ");
                }

            }
            else
            {
                Response.Write("No File Upload!");
            }
      Response.End();
}

封装文件实体类的部分代码:

/// <summary>
        /// 文档实体
        ///</summary>
        public FileEntity()
        {
        }

        private int did;//ID
        private string doctitle;//标题
        private string doctype;//文档类型
        private DateTime docdate;//日期
        private string docurl;//保存地址
        private string docstate;//状态

 

 如果遇到浏览器不兼容问题,去点聚的官网论坛有解放方法,可以下载相关文件,地址如下:

http://forum.dianju.cn/viewtopic.php?f=3&t=1041