DSOFramer的使用 .

http://blog.csdn.net/phqm/article/details/2120084

DSOFramer的使用 .
分类: C#2.0 2008-02-25 21:28 4260人阅读 评论(6) 收藏 举报
一、先注册一下DSOFramer.ocx

    操作:将DSOFramer.ocx复制到C:/windows/system32目录下,

         开始->运行->regsvr32 DSOFramer.ocx , 系统会提示DSOFramer.ocx中的DllRegisterServer成功。

二、添加DSOFramer.ocx到你的项目中

    操作:先说明一下,我用VS 2005 ,其他VS版本可能操作会有不同,操作应该也类似自己试试,问题应该不大。
          在你要访问DSOFramer.ocx的目录上点选右键菜单中的“添加现有项”,找到DSOFramer.ocx,确定。


三、在网页中加载DSOFramer

     新建Office.aspx

    添加如下代码:
     <object id="MyOffice" name = "MyOffice" style="LEFT: 0px; WIDTH: 100%; TOP: 0px; HEIGHT: 100%"
    classid="clsid:00460182-9E5E-11D5-B7C8-B8269041DD57" codebase="dsoframer.ocx#version=2,2,0,0" >
    </object>

     [注]:VS 2005对语法的要求贼多,什么ID的值要用""括起来啦,<object>不能大写啦,……
           没什么大碍,但是很烦人,简直就是微软版的唐僧,我给大家提供的代码是修改过的,VS不会有哪些废话了。

     然后再body中加入onload事件的处理函数

                   <body onload="show_word();">

     再在<head></head>中间加入函数体


     <script language="javascript" type="text/javascript">
        <!--

        function show_word() {
                var str=window.location.search;
                var pos_start=str.indexOf("id")+3;
                if (pos_start == 2)
                return ;
       
                var id = "http://localhost/Getdc.aspx?id=" + str.substring(pos_start);
                document.all. MyOffice.Open( id,false, "Word.Document");
        }

        // -->
        </script>


四、编制Getdc.aspx.cs文件
      建立Getdc.aspx文件,VS会同时建立与之关联的Getdc.aspx.cs文件

      先加入命名空间
        using System.Data.SqlClient;
        using System.Data.SqlTypes;

      编辑Getdc.aspx.cs的Page_Load函数;
      protected void Page_Load(object sender, EventArgs e)
      {
        int pid = Convert.ToInt32(Request["id"]);

        SqlConnection myConnection = new SqlConnection("Data Source=/"localhost/";Initial Catalog=/"demo/";Persist Security Info=True;User ID=demo;Password=demo");//数据库的相关设置自己改吧,我就不废话了
        SqlCommand mycommand = myConnection.CreateCommand();
        myConnection.Open();

        mycommand.CommandText = "SELECT filedata " +
            " FROM Table_word WHERE (ID = " + pid.ToString() + ") ";//其中filedata的数据库类型是varbinary(MAX)
        SqlDataReader myReader = mycommand.ExecuteReader();
        myReader.Read();
        SqlBinary binaryStream = myReader.GetSqlBinary(0);
        myReader.Close();
        myConnection.Close();

        Response.BinaryWrite(binaryStream.Value);
    }

     至此,只要你指定的ID没问题,就应该可以加载Word文档了。



五、建立保存文档的按钮


        在Office.aspx中添加按钮

        <input id="Button1" type="submit" value="保存" onclick="return Submit_upload_onclick()" />

        再建立一个input

            <input type="file" name="File" id = "File"/>


        然后添加Submit_upload_onclick()函数:

        function Submit_upload_onclick() {
        var str=window.location.search;
        var pos_start=str.indexOf("id")+3;
        if (pos_start == 2)
        return ;

        document.all.MyOffice.HttpInit();
        document.all.MyOffice.HttpAddPostCurrFile("File", "");
        var id = "http://localhost/Savedc.aspx?id=" + str.substring(pos_start);
        document.all.MyOffice.HttpPost(id);
        }

六、编制Savedc.aspx.cs文件
      建立Savedc.aspx文件,VS会同时建立与之关联的Savedc.aspx.cs文件

      先加入命名空间
        using System.Data.SqlClient;
        using System.Data.SqlTypes;

      编辑Savedc.aspx.cs的Page_Load函数;

    protected void Page_Load(object sender, EventArgs e)
    {
        int pid = Convert.ToInt32(Request["id"]);
        Byte[] source_bin = Request.BinaryRead(Request.TotalBytes);

        //---------------------------------------------------------------------------------------
       int i, loop, again = -1, file_begin = -1;
         for (i = 0; i < Request.TotalBytes; i++)
        {
            if (source_bin[i] == 13)
                if (source_bin[i + 1] == 10)
                    break;
        }
        Byte[] MyHeader = new Byte[i];
        for (loop = 0; loop < i; loop++)
            MyHeader[loop] = source_bin[loop];

        for (i += 2; i < Request.TotalBytes; i++)
        {
            if (source_bin[i] == 13)
                if (source_bin[i + 1] == 10)
                    if (source_bin[i + 2] == 13)
                        if (source_bin[i + 3] == 10)
                            break;
        }

        file_begin = i + 4;
        for (i = file_begin; i < Request.TotalBytes; i++)
        {
            for (loop = 0; loop < MyHeader.Length; loop++)
                if (source_bin[i + loop] != MyHeader[loop])
                    break;
            if (loop >= MyHeader.Length)
            {
                break;
            }
        }

        Byte[] result = new Byte[i - file_begin];
        //这是个不得已的办法,用循环肯定会慢,但我不会其他方法
        //希望高手完善
        for (loop = file_begin; loop < i - file_begin; loop++)
            result[loop - file_begin] = source_bin[loop];

        //---------------------------------------------------------------------------------------
        //以上代码将word文档从二进制流中提取出来,存储在result[]中,方法虽笨,肯定好使
        //本人不懂VB,更不懂Java,
        //上面代码是我楞从梁无惧用VB写的无惧上传类V2.0里挑选有用的部分翻译成C#的,泪ing……
        //看看人家梁兄,多无私,给同行提供这么好的东东
        //我在网上找了N + 1天,就TM没找到C#版的,再泪ing……
        //现在提供给大家,希望我是最后一个为此郁闷的人


        SqlConnection myConnection = new SqlConnection("Data Source=/"localhost/";Initial Catalog=/"demo/";Persist Security Info=True;User ID=demo;Password=demo");//数据库的相关设置自己改吧
        SqlCommand mycommand = myConnection.CreateCommand();
        mycommand.CommandText = "UPDATE Table_word SET filedata = @myfiledata , b_finished = 1 WHERE (ID = @myID)";
        mycommand.Parameters.Add("@myfiledata", SqlDbType.VarBinary, 0, "filedata");
        mycommand.Parameters.Add("@myID", SqlDbType.Int, 4, "ID");

        SqlDataAdapter mydpt;
        DataSet myds = new DataSet();

        mydpt = new SqlDataAdapter("SELECT ID ,filedata , b_finished FROM Table_copy  WHERE (ID = " + Request["id"] + ")", myConnection);
        myConnection.Open();

        mydpt.UpdateCommand = mycommand;
        mydpt.Fill(myds);
        DataTable mydt = myds.Tables[0];
        DataRow myrow;
        myrow = mydt.Rows[0];
        myrow.BeginEdit();
        myrow["filedata"] = result;
        myrow.EndEdit();

        mydpt.Update(myds);
        myConnection.Close();/**/
          }


        至此,只要你指定的ID没问题,就应该可以保存Word文档了。




编辑Savedc.aspx.cs的Page_Load函数;
protected void Page_Load(object sender, EventArgs e)
------------------------------------------------------------------------------
果然是ASP的写法。不过c#里面不需要这样,C# 提供了这样的接口

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Clear();
                //ID为文档的主键,如果ID不为空,则更新数据,否则新建一条记录
        string ID = Request.Params["ID"];
        string DocID,DocTitle,DocType;
        DocID = "test";
        DocTitle = "test";
        if(ID != null && ID !=""){
            DocID = Request.Params["DocID"];
            DocTitle = Request.Params["DocTitle"];
        }
        DocType = Request.Params["DocType"];
        if(DocType == "")
            DocType =  "doc";

        DocType = DocType.Substring(0, 3);
        if (Request.Files.Count > 0)
        {
                OleDbConnection objConnection;
   
                HttpPostedFile upPhoto = Request.Files[0];
                int upPhotoLength = upPhoto.ContentLength;
                byte[] PhotoArray = new Byte[upPhotoLength];
                Stream PhotoStream = upPhoto.InputStream;
                PhotoStream.Read(PhotoArray, 0, upPhotoLength); //这些编码是把文件转换成二进制的文件

                string strConnection = "Provider=Microsoft.Jet.OleDb.4.0;";
                strConnection += @"Data Source=" + this.Server.MapPath("des.mdb");
                objConnection = new OleDbConnection(strConnection);
                objConnection.Open();

                string strSql;
                if (ID != null && ID != "")
                {
                    strSql = "update Doc Set DocContent  = @FImage where id = " + ID;                
                    OleDbCommand comd = new OleDbCommand(strSql, objConnection); //执行sql语句
                    comd.Parameters.Add("@FImage", OleDbType.Binary);
                    comd.Parameters["@FImage"].Value = PhotoArray;
                    comd.ExecuteNonQuery(); //执行查询   
                   
                }
                else
                {
                    strSql = "Insert into Doc(DocID,DocTitle,DocType,DocContent) values(@DocId,@DocTitle,@DocType,@FImage)";
                    OleDbCommand comd = new OleDbCommand(strSql, objConnection); //执行sql语句
                    if(DocID != "")
                        comd.Parameters.Add("@DocId", OleDbType.VarChar, 20).Value = DocID;  //定义参数同时给它赋值
                    if (DocTitle != "")
                        comd.Parameters.Add("@DocTitle", OleDbType.VarChar, 50).Value = DocTitle;
                    comd.Parameters.Add("@DocType", OleDbType.VarChar, 10).Value = DocType;
                    comd.Parameters.Add("@FImage", OleDbType.Binary);
                    comd.Parameters["@FImage"].Value = PhotoArray;
                    comd.ExecuteNonQuery(); //执行查询
                }
                objConnection.Close();  //关闭数据库
                Response.Write("OK");
                Response.End();
            //-------------------------------------------
        }else{
            Response.Write("No File Upload!");
        }
    }
}

你可能感兴趣的:(Office,DSO,DSO)