【发布时间】:2012-07-08 07:02:20
【问题描述】:
通过使用OpenXML 来操作Word 文档(作为模板),服务器应用程序将新内容保存为临时文件,然后将其发送给用户下载.
问题是如何使这些内容准备好下载而不将其作为临时文件保存在服务器上?是否可以将 OpenXML 结果保存为 byte[] 或 Stream 而不是保存为文件?
【问题讨论】:
标签: openxml
通过使用OpenXML 来操作Word 文档(作为模板),服务器应用程序将新内容保存为临时文件,然后将其发送给用户下载.
问题是如何使这些内容准备好下载而不将其作为临时文件保存在服务器上?是否可以将 OpenXML 结果保存为 byte[] 或 Stream 而不是保存为文件?
【问题讨论】:
标签: openxml
您可以创建 WordprocessingDocument,然后使用Save() 方法将其保存到Stream。
【讨论】:
使用此页面: 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();
}
}
}
【讨论】: