2012-05-19 7 views
16

私が作成したワードドキュメントをブラウザにストリーミングできません。私は絶えずMicrosoft Wordから文書が壊れているというメッセージを受け取ります。メモリにストリーミングするOpenXML SDKを使用しているASP.NETドキュメントでASP.NETが破損しています。

コンソールアプリケーション経由でコードを実行し、ASP.NETを画像から取り除くと、ドキュメントは問題なく正しく生成されます。私はすべてがファイルを書き込むことを中心に考えています。しかし、何もかなり機能していない - 私はlinksの多くでlookedを持って

using (MemoryStream mem = new MemoryStream()) 
      { 
       // Create Document 
       using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(mem, 
      WordprocessingDocumentType.Document, true)) 
       { 
        // Add a main document part. 
        MainDocumentPart mainPart = wordDocument.AddMainDocumentPart(); 

        new Document(new Body()).Save(mainPart); 

        Body body = mainPart.Document.Body; 
        body.Append(new Paragraph(
           new Run(
            new Text("Hello World!")))); 

        mainPart.Document.Save(); 
        // Stream it down to the browser 

        // THIS IS PROBABLY THE CRUX OF THE MATTER <--- 
        Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx"); 
        Response.ContentType = "application/vnd.ms-word.document"; 
        mem.WriteTo(Response.OutputStream); 
        Response.End(); 
       } 

      } 

は、ここに私のコードです。私は多くの人がMemoryStream.WriteToを使用し、BinaryWriteを使用しています。この時点では、正しい方法が何であるか分かりません。また、長いコンテンツタイプ、つまりapplication/vnd.openxmlformats-officedocument.wordprocessingml.documentを試しましたが、運はありません。 )(使用して内

..: -

いくつかのスクリーンショットは、あなたが回復しようとする場合でも、あなたはこの質問につまずく人のために、同じ「部品が欠落しているか、または無効です」

ソリューションを取得しますWordProcessingDocumentのディレクティブを呼び出す必要があります。

wordDocument.Save();また、正しく外使用のブロックでこれを使用し、MemoryStreamををストリーミングする

Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; 
       Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx"); 
       mem.Position = 0; 
       mem.CopyTo(Response.OutputStream); 
       Response.Flush(); 
       Response.End(); 

enter image description here enter image description here

+0

どのようにwordDocument.Save()を追加しますか? ?私は試してみました。それに近い唯一のコードは.Close()で、それは私にとってはうまくいきません。 – RicL

+1

あなたの質問と回答のためにkd7に感謝します。 –

答えて

7

使用CopyTo代わりに、それは書き込みに失敗しますWriteToにバグがありますターゲットストリームがすべてを一度に書き込むことをサポートしていない場合、バッファの内容全体。

1

あなたのContentType値が間違っていると思います。つまり、Word 97 - 2003形式です。これを次のように変更してください:

application/vnd.openxmlformats-officedocument.wordprocessingml.document 

この問題が解決するかどうかを確認してください。

2

あなたのコードをコピーして貼り付け、 "wordDocument.close();"それが追加され、機能しました(私はアクションを狙ったAsp.NET MVCでやった)

2

.NET Framework 3.5以下の亜種。このバージョンのフレームワークでは、StreamクラスのCopyToメソッドはありません。したがって、のwriteToは、次のコードで置き換えられる方法:

   byte[] arr = documentStream.ToArray(); 
       fileStream.Write(arr, 0, arr.Length); 

例はロディオンの答えに拡大し、質問でこれを使用する変数を一致させるにはhttp://blogs.msdn.com/b/mcsuksoldev/archive/2010/04/09/creating-a-new-microsoft-word-document-from-a-template-using-openxml.aspx

0

によって発見されましたが私のために働いていたものです:

Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; 
Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx"); 
mem.Position = 0; 
byte[] arr = mem.ToArray(); 
Response.BinaryWrite(arr); 
Response.Flush(); 
Response.End(); 
関連する問題