Asp.net mvc 使用ASP.NET MVC 4在OpenXML SDK中对Word文档进行流式处理会导致文档损坏

Asp.net mvc 使用ASP.NET MVC 4在OpenXML SDK中对Word文档进行流式处理会导致文档损坏,asp.net-mvc,ms-word,openxml-sdk,Asp.net Mvc,Ms Word,Openxml Sdk,我正在尝试在ASP.NET MVC 4上执行此操作: MemoryStream mem = new MemoryStream(); using (WordprocessingDocument wordDoc = WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true)) {

我正在尝试在ASP.NET MVC 4上执行此操作:

MemoryStream mem = new MemoryStream();
        using (WordprocessingDocument wordDoc =
            WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
        {
            // instantiate the members of the hierarchy
            Document doc = new Document();
            Body body = new Body();
            Paragraph para = new Paragraph();
            Run run = new Run();
            Text text = new Text() { Text = "The OpenXML SDK rocks!" };

            // put the hierarchy together
            run.Append(text);
            para.Append(run);
            body.Append(para);
            doc.Append(body);

            //wordDoc.Close();

            ///wordDoc.Save();
        }


return File(mem.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx");
但是,ABC.docx打开时已损坏,即使修复后也无法打开

有什么想法吗

链接Qs:


显然,问题在于缺少这两行:

wordDoc.AddMainDocumentPart();
wordDoc.MainDocumentPart.Document = doc;
将代码更新到下面,它现在可以完美地工作,甚至不需要任何额外的刷新等

MemoryStream mem = new MemoryStream();
        using (WordprocessingDocument wordDoc =
            WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
        {
            wordDoc.AddMainDocumentPart();
            // instantiate the members of the hierarchy
            Document doc = new Document();
            Body body = new Body();
            Paragraph para = new Paragraph();
            Run run = new Run();
            Text text = new Text() { Text = "The OpenXML SDK rocks!" };

            // put the hierarchy together
            run.Append(text);
            para.Append(run);
            body.Append(para);
            doc.Append(body);
            wordDoc.MainDocumentPart.Document = doc;
            wordDoc.Close();
        }
return File(mem.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx");

显然,问题在于缺少这两行:

wordDoc.AddMainDocumentPart();
wordDoc.MainDocumentPart.Document = doc;
将代码更新到下面,它现在可以完美地工作,甚至不需要任何额外的刷新等

MemoryStream mem = new MemoryStream();
        using (WordprocessingDocument wordDoc =
            WordprocessingDocument.Create(mem, DocumentFormat.OpenXml.WordprocessingDocumentType.Document, true))
        {
            wordDoc.AddMainDocumentPart();
            // instantiate the members of the hierarchy
            Document doc = new Document();
            Body body = new Body();
            Paragraph para = new Paragraph();
            Run run = new Run();
            Text text = new Text() { Text = "The OpenXML SDK rocks!" };

            // put the hierarchy together
            run.Append(text);
            para.Append(run);
            body.Append(para);
            doc.Append(body);
            wordDoc.MainDocumentPart.Document = doc;
            wordDoc.Close();
        }
return File(mem.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document", "ABC.docx");