使用Word Interop C#打开word文档时出错

时间:2013-05-03 09:42:10

标签: c# asp.net sharepoint

enter image description here

当我下载word文档时,这种情况不断出现,但是当我重新打开它时,它可以毫无问题地工作。

这是我的代码:

                object missing = Missing.Value;
            object start1 = 0;

            var wordApp = new ApplicationClass();

            Microsoft.Office.Interop.Word.Document myDoc = wordApp.Documents.Add(ref missing, ref missing,
                                                                                 ref missing, ref missing);

            object doNotSaveChanges = WdSaveOptions.wdDoNotSaveChanges;
            //object fileStream = new FileStream(Server.MapPath("~/Upload/") + fileName,
            //                                                  FileMode.Create);

            object fs = Server.MapPath("~/Upload/") + fileName;

            Range rng = myDoc.Range(ref start1, ref missing);

            try
            {
                myDoc.Application.ActiveDocument.SaveAs(ref fs, ref missing, ref missing, ref missing,
                                                        ref missing, ref missing, ref missing, ref missing,
                                                        ref missing, ref missing,
                                                        ref missing, ref missing, ref missing, ref missing,
                                                        ref missing, ref missing);

                wordApp.Visible = true;



    //All document information code here
    }
            catch (Exception)
            {

                throw;
            }
            finally
            {
                myDoc.Save();

                myDoc.Close(ref doNotSaveChanges, ref missing, ref missing);
            }

            // The file name used to save the file to the client's system..

            string filename = fs.ToString();
            System.IO.Stream stream = null;
            try
            {
            // Open the file into a stream. 
                stream = new FileStream(filename, System.IO.FileMode.Open, System.IO.FileAccess.Read,
                                        System.IO.FileShare.Read);
            // Total bytes to read: 
                long bytesToRead = stream.Length;
                Response.ContentType = "application/msword";
                Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
            // Read the bytes from the stream in small portions. 
                while (bytesToRead > 0)
                {
            // Make sure the client is still connected. 
                    if (Response.IsClientConnected)
                    {
           // Read the data into the buffer and write into the 
            // output stream. 
                        byte[] buffer = new Byte[10000];
                        int length = stream.Read(buffer, 0, 10000);
                        Response.OutputStream.Write(buffer, 0, length);
                        Response.Flush();
            // We have already read some bytes.. need to read 
            // only the remaining. 
                        bytesToRead = bytesToRead - length;
                    }
                    else
                    {
            // Get out of the loop, if user is not connected anymore.. 
                        bytesToRead = -1;
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            // An error occurred.. 
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
            }
    }

1 个答案:

答案 0 :(得分:0)

要修复损坏的Word文件,您应该尝试第三方软件RecoveryFix for Word恢复工具。此工具将帮助您恢复Word文件。

相关问题