asp.net操作word文件
上传word文件
        public int SaveOrUpdatePhoto(FileUpload FileUp, string TdDh, string QyID)
        {
           
            int FileLength = FileUp.PostedFile.ContentLength;
            string FileType = FileUp.PostedFile.ContentType;
            if (FileLength == 0)
            {
                return 2;
            }
            else if (FileType != "application/msword")
            {
                return 3;
            }
            else
            {
                try
                {
                    byte[] Document = new byte[FileLength];
                    Stream ObjectStream = FileUp.PostedFile.InputStream;
                    int readedLengh = ObjectStream.Read(Document, 0, FileLength);
                    string Type = FileUp.PostedFile.ContentType;
                    if (readedLengh != FileLength)
                    {
                        return 0;
                    }
                    Connection connstr = new Connection();
                    string cnstr = connstr.GetConnectionString();
               
                    SqlConnection cn = new SqlConnection(cnstr);
                    cn.Open();
                    string sql = "update TDJBXXB Set XcApDocument = @Document Where QYID=@QyID And TDDH = @TdDh";
                    SqlCommand cmd = new SqlCommand(sql, cn);
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@TdDh", SqlDbType.VarChar, 15).Value = TdDh;
                    cmd.Parameters.Add("@Document", SqlDbType.Image, FileLength).Value = Document;
                    cmd.Parameters.Add("@QyID", SqlDbType.VarChar, 30).Value = QyID;
                    cmd.Parameters["@TdDh"].Direction = ParameterDirection.Input;
                    cmd.Parameters["@Document"].Direction = ParameterDirection.Input;
                    cmd.Parameters["@QyID"].Direction = ParameterDirection.Input;
            
                    cmd.ExecuteNonQuery();
                    ObjectStream.Flush();
                    ObjectStream.Dispose();
                    ObjectStream.Close();
                    cn.Close();
                    return 1;
                }
                catch (Exception ex)
                {
                    bool rethrow = ExceptionPolicy.HandleException(ex, "DataAccess Layer Exception Policy");
                    if (rethrow)
                        throw;
                    return 0;
                }
            }
        }
        public int GetXcApDocument(string QyID, string TdDh, Page page)
        {
            try
            {
                Connection con = new Connection();
                string cnstr = con.GetConnectionString();
                SqlConnection conn = new SqlConnection(cnstr);
                string sql = "Select XcApDocument From TDJBXXB Where QYID=@QYID And TDDH=@TDDH";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.Add("@QYID", SqlDbType.VarChar).Value = QyID;
                cmd.Parameters.Add("@TDDH", SqlDbType.VarChar).Value = TdDh;
                cmd.Parameters["@QYID"].Direction = ParameterDirection.Input;
                cmd.Parameters["@TDDH"].Direction = ParameterDirection.Input;
                FileStream fs;
                BinaryWriter bw;
                //设定允许读取到缓冲区的最大长度
                int buffersize = 100;
                //要将字节流读入的缓冲区
                byte[] outbyte = new byte[buffersize];
                //用于记录已经读取的字节数
                long reval;
                //字段中的索引,从这里开始读取操作
                long startIndex;
                //FileStream对象将封装的文件的相对路径或绝对路径
                string filePath = null;
                conn.Open();
                SqlDataReader reader;
                reader = cmd.ExecuteReader();
                if (!reader.HasRows)
                {
                    return 2;
                }
                while (reader.Read())
                {
                    filePath = page.Request.PhysicalApplicationPath + "../行程安排.doc";
                    fs = null;
                    bool locked = true;
                    while (locked)
                    {
                        try
                        {
                            fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
                            locked = false;
                            bw = new BinaryWriter(fs);
                            startIndex = 0;
                            //将字节流读入outbyte缓冲区中并返回读取的字节数
                            reval = reader.GetBytes(0, startIndex, outbyte, 0, buffersize);
                            //当读取的字节流达到缓冲区允许的最大长度时要卸载缓冲区内的数据并将数据写入文件
                            while (reval == buffersize)
                            {
                                bw.Write(outbyte);
                                bw.Flush();
                                //重新设定开始读取的位置,并继续读取和写数据
                                startIndex += buffersize;
                                reval = reader.GetBytes(0, startIndex, outbyte, 0, buffersize);
                            }
                            //将缓冲区内最后剩余的数据写入文件
                            bw.Write(outbyte, 0, (int)reval - 1);
                            bw.Flush();
                            bw.Close();
                            fs.Close();
                        }
                        catch
                        {
                            System.Threading.Thread.Sleep(100);
                        }
                    }
                }
                reader.Close();
                conn.Close();
                GetDocument(filePath, page);
                return 0;
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "DataAccess Layer Exception Policy");
                if (rethrow)
                    throw;
                return 1;
            }
        }
        
        private void GetDocument(string filename, Page page)
        {
            //string path = Server.MapPath(this.xlfile.Text + ".xls");
            System.IO.FileInfo file = new System.IO.FileInfo(filename);
            page.Response.Clear();
            page.Response.Charset = "GB2312";
            page.Response.ContentEncoding = System.Text.Encoding.UTF8;
            //   添加头信息,为"文件下载/另存为"对话框指定默认文件名   
            page.Response.AddHeader("Content-Disposition", "attachment;   filename=" + Server.UrlEncode(file.Name));
            //   添加头信息,指定文件大小,让浏览器能够显示下载进度   
            page.Response.AddHeader("Content-Length", file.Length.ToString());
            //   指定返回的是一个不能被客户端读取的流,必须被下载   
            page.Response.ContentType = "application/octet-stream";
            //   把文件流发送到客户端   
            page.Response.WriteFile(file.FullName);
            //   停止页面的执行   
            page.Response.End();
        }
                    
                
        
            
                
            
        
浙公网安备 33010602011771号