没有临时文件的OpenXML文件下载

Kue*_*per 6 c# asp.net openxml openxml-sdk

有没有办法在ASP.Net页面中为新生成的OpenXML(docx)文件提供下载而不将其保存在临时文件夹中?

MSDN上,我只找到了使用临时文件的教程,但我想过使用WordprocessingDocument.MainDocumentPart.GetStream()并直接写出流.

Sam*_*eff 12

创建文档时,使用a MemoryStream作为后备存储.然后正常创建和关闭文档,并将内存流的内容提供给客户端.

using(var stream = new MemoryStream())
{
    using(var doc = WordprocessingDocument.Create(stream, WordprocessingDocumentType.Document, true) 
    {
        ...
    }
    stream.Position = 0;
    stream.CopyTo(Response.OutputStream);
}
Run Code Online (Sandbox Code Playgroud)

不要只是抓住MainDocumentPart因为,顾名思义,这只是文档包的一部分,而不是一切.

您还需要为内容类型和处置设置响应标头.