【问题标题】:Streaming OpenXML result流式传输 OpenXML 结果
【发布时间】:2012-07-08 07:02:20
【问题描述】:

通过使用OpenXML 来操作Word 文档(作为模板),服务器应用程序将新内容保存为临时文件,然后将其发送给用户下载.

问题是如何使这些内容准备好下载而不将其作为临时文件保存在服务器上?是否可以将 OpenXML 结果保存为 byte[]Stream 而不是保存为文件?

【问题讨论】:

    标签: openxml


    【解决方案1】:

    您可以创建 WordprocessingDocument,然后使用Save() 方法将其保存到Stream

    http://msdn.microsoft.com/en-us/library/cc882497

    【讨论】:

      【解决方案2】:

      使用此页面: OpenXML file download without temporary file

      我把我的代码改成了这个:

      byte[] result = null;
      byte[] templateBytes = System.IO.File.ReadAllBytes(wordTemplate);
      using (MemoryStream templateStream = new MemoryStream())
      {
          templateStream.Write(templateBytes, 0, (int)templateBytes.Length);
          using (WordprocessingDocument doc = WordprocessingDocument.Open(templateStream, true))
          {
              MainDocumentPart mainPart = doc.MainDocumentPart;           
              ...         
              mainPart.Document.Save();
              templateStream.Position = 0;
              using (MemoryStream memoryStream = new MemoryStream())
              {
                  templateStream.CopyTo(memoryStream);
                  result = memoryStream.ToArray();
              }
          }
      }
      

      【讨论】:

      • 如果仅供下载,无需转换为byte[]。只需将流位置设置为 0。
      猜你喜欢
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 2020-07-17
      • 2017-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-23
      相关资源
      最近更新 更多