2013-04-24 124 views
6

我正在創建一個樣本處理程序來生成簡單的Word文檔。
該文件將包含文本世界,你好用Open XML創建Word文檔

這是我創建的Word文檔,我使用的代碼(C#.NET 3.5),
但在它沒有文本,大小爲0。
我該如何解決它?
(我用CopyStream方法,因爲CopyTo從在.NET 4.0及以上版本纔可用。)

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 Productivity Tool來調試文檔。還要考慮首先在Word中創建文檔,然後使用該工具爲您提供將創建文檔的代碼。 – juharr 2013-04-24 14:36:34

回答

2

我已經體改你的代碼,使其工作。保存下載後,我可以正確打開它。 請參閱我的修改如下。希望這個幫助。

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 2015-11-28 19:48:01

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

太好了。我在這裏發現類似的解決方案,但看起來很晚。 – 2013-04-24 16:11:34

+0

我們可以考慮一些我們在這裏丟失的觀點:wordProcessingDocument需要在發送到流之前關閉。流應該從頭開始。感謝您在這裏發佈的解決方案。 – 2013-04-24 16:29:31