通過使用的OpenXML來操縱的Word文檔(作爲模板),服務器應用程序將保存新的內容作爲一個臨時文件,然後將其發送給用戶下載。流的OpenXML導致
問題是如何使這些內容準備好下載而不必將其作爲臨時文件保存在服務器上?是否可以將OpenXML結果保存爲byte[]
或Stream
而不是將其保存爲文件?
通過使用的OpenXML來操縱的Word文檔(作爲模板),服務器應用程序將保存新的內容作爲一個臨時文件,然後將其發送給用戶下載。流的OpenXML導致
問題是如何使這些內容準備好下載而不必將其作爲臨時文件保存在服務器上?是否可以將OpenXML結果保存爲byte[]
或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();
}
}
}
您可以創建WordprocessingDocument,然後使用Save()
方法將其保存到Stream
。
如果它只是下載無需轉換爲byte []。只需將流位置設置爲0。 – user3285954 2017-06-15 08:56:31