zoukankan      html  css  js  c++  java
  • 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();

    http://www.cnblogs.com/9who/archive/2008/07/28/1254532.html

  • 相关阅读:
    .NET日期格式化
    Win7 计算机(我的电脑)右键菜单“管理”打不开,解决方法
    没有对“Temporary ASP.NET Files”的写访问权限
    Android安装jsk出错
    WPF 处理 系统Scale参数
    WPF WindowChrome 自定义标题栏时窗体阴影效果设置
    使用WindowChrome 在切换ResizeMode值时的问题
    Vue.js provide / inject 踩坑
    MYSQL 查询日期最大的那条记录
    所有子一级元素添加阴影
  • 原文地址:https://www.cnblogs.com/y0umer/p/3839098.html
Copyright © 2011-2022 走看看