asp.net与word文档在线

1.通过javascript打开\编辑\根据模板新建word文档           

           //"SharePoint.OpenDocuments.1"可与Office XP兼容
            var openDocObj = new ActiveXObject("SharePoint.OpenDocuments.2");
           
            //打开文档
            openDocObj.ViewDocument(lUrl+"./documents/sample.doc");

            //ViewDocument()方法还有一个重载签名,可以让我们手工指定激活哪个程序来打开文档:     
            openDocObj.ViewDocument(lUrl+"./documents/sample.doc",   要激活的程序的ProgID);  

            //编辑文档
            var lUrl = window.location.href;
            openDocObj.EditDocument(lUrl+"./documents/sample.doc");
           
            //根据模板创建文档(模板,新文档保存路径)
            openDocObj.CreateNewDocument(lUrl+"./documents/sampleTemplate.dot", lUrl+"./documents/"); 

    注:iis必须设置为可写,web服务扩展中的WebDaV应是允许状态,如图:    

2.直接把文件上传进数据库

string FileName;
        Stream WordStream = SearchFile.PostedFile.InputStream;
        string FilePath = this.SearchFile.PostedFile.FileName;
        FileName = Path.GetFileName(FilePath);
        if (FileName != null && FileName != "")
        {
            int WordLen = SearchFile.PostedFile.ContentLength;
            string WordType = SearchFile.PostedFile.ContentType;
            byte[] WordData = new Byte[WordLen];
            int n = WordStream.Read(WordData, 0, WordLen);
            WordStream.Close();
            SqlCommand com = new SqlCommand();
            com.CommandText = "insert into MyTable(name,FileBinary) values(@FileName,@FileBinary)";
            com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileName", System.Data.SqlDbType.Char, 20, "FileName"));
            com.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FileBinary", System.Data.SqlDbType.Image, WordData.Length, "FileBinary"));
            com.Connection = sqlConnection;
            com.Parameters["@FileName"].Value = FileName;
            com.Parameters["@FileBinary"].Value = WordData;
            com.Connection.Open();
            com.ExecuteNonQuery();
            com.Connection.Close();
        }
        else
        {
            Response.Write(" ");
        }

3.数据流的方式在浏览器中显示Word文件

Response.ContentType = "Application/msword";

        this.Response.Clear();
       
        SqlCommand selcom = new SqlCommand();
        selcom.CommandText = "select name,FileBinary from MyTable order by id desc";
        selcom.Connection = sqlConnection;
        selcom.Connection.Open();
        SqlDataReader dr = selcom.ExecuteReader();
        dr.Read();
        Byte[] b = new Byte[(dr.GetBytes(1, 0, null, 0, int.MaxValue))];
        dr.GetBytes(1, 0, b, 0, b.Length);
        dr.Close();
        selcom.Connection.Close();
        System.IO.Stream fs = this.Response.OutputStream;
        fs.Write(b, 0, b.Length);

        fs.Close();
        this.Response.End();

posted on 2008-07-28 11:45  9who  阅读(5461)  评论(0编辑  收藏  举报

导航