2013-04-24 13 views
6

サンプルハンドラを作成して、簡単なWord文書を生成します。
この文書はなりこんにちはOpen XMLでワード文書を作成する

テキストこれは私が使用するコード(C#.NET 3.5)で含まれ、
私が作成したWord文書を得たが、それには、テキスト、サイズが0ではありません。
どうすれば修正できますか?
(CopyToのは、.NET 4.0でのみ上利用可能であるので、私はCopyStreamメソッドを使用します。)

public class HandlerCreateDocx : IHttpHandler 
{ 
    public void ProcessRequest(HttpContext context) 
    { 
     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(); 

       // Create the document structure and add some text. 
       mainPart.Document = new Document(); 
       Body body = mainPart.Document.AppendChild(new Body()); 
       Paragraph para = body.AppendChild(new Paragraph()); 
       Run run = para.AppendChild(new Run()); 
       run.AppendChild(new Text("Hello world!")); 
       mainPart.Document.Save(); 
       // Stream it down to the browser 
       context.Response.AppendHeader("Content-Disposition", "attachment;filename=HelloWorld.docx"); 
       context.Response.ContentType = "application/vnd.ms-word.document"; 
       CopyStream(mem, context.Response.OutputStream); 
       context.Response.End(); 
      } 
     } 
    } 

    // Only useful before .NET 4 
    public void CopyStream(Stream input, Stream output) 
    { 
     byte[] buffer = new byte[16 * 1024]; // Fairly arbitrary size 
     int bytesRead; 

     while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) 
     { 
      output.Write(buffer, 0, bytesRead); 
     } 
    } 
} 
+1

Open XML生産性ツールを使用してドキュメントをデバッグすることをお勧めします。また、Wordで文書を最初に作成し、文書を作成するコードを提供するツールを使用することも検討してください。 – juharr

答えて

2

私はそれを動作させるためにあなたのコードをmodifedています。私はそれをダウンロードした後に正しく開くことができます。 下記の変更をご覧ください。この助けを願っています。

using (MemoryStream documentStream = new MemoryStream()) 
{ 
    using (WordprocessingDocument myDoc = WordprocessingDocument.Create(documentStream, WordprocessingDocumentType.Document, true)) 
    { 
     // Add a new main document part. 
     MainDocumentPart mainPart = myDoc.AddMainDocumentPart(); 
     //Create Document tree for simple document. 
     mainPart.Document = new Document(); 
     //Create Body (this element contains 
     //other elements that we want to include 
     Body body = new Body(); 
     //Create paragraph 
     Paragraph paragraph = new Paragraph(); 
     Run run_paragraph = new Run(); 
     // we want to put that text into the output document 
     Text text_paragraph = new Text("Hello World!"); 
     //Append elements appropriately. 
     run_paragraph.Append(text_paragraph); 
     paragraph.Append(run_paragraph); 
     body.Append(paragraph); 
     mainPart.Document.Append(body); 

     // Save changes to the main document part. 
     mainPart.Document.Save(); 
     myDoc.Close(); 
     context.Response.ClearContent(); 
     context.Response.ClearHeaders(); 
     context.Response.ContentEncoding = System.Text.Encoding.UTF8; 
     SetContentType(context.Request, context.Response, "Simple.docx"); 
     documentStream.Seek(0, SeekOrigin.Begin); 
     documentStream.CopyTo(context.Response.OutputStream); 
     context.Response.Flush(); 
     context.Response.End(); 
    } 
} 
+0

あなたの答えは冗長です。そして、行動を変えない多くの無駄な行を私たちに投げかけます。私はあなたの答えを削除することをお勧めします。 – SandRock

8

これは私のために、外部のUSINGブロックにストリーミングコードを挿入することで動作します。

これにより、WordprocessingDocument.Close(Disposeメソッド経由)が呼び出されます。

public void ProcessRequest(HttpContext context) 
{ 
    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(); 

      // Create the document structure and add some text. 
      mainPart.Document = new Document(); 
      Body body = mainPart.Document.AppendChild(new Body()); 
      Paragraph para = body.AppendChild(new Paragraph()); 
      Run run = para.AppendChild(new Run()); 
      run.AppendChild(new Text("Hello world!")); 
      mainPart.Document.Save(); 
     } 

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

私はあなたがここに投稿するのと似たような解決策を見つけましたが、かなり遅く感じます。 –

+0

ここで欠落している点のいくつかを考えてみましょう。wordProcessingDocumentをストリームに送信する前に閉じておく必要があります。ストリームは開始時に開始する必要があります。ここにあなたのポスト・ソリューションをありがとう。 –

関連する問題