Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8986217
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:29:53+00:00 2026-06-15T21:29:53+00:00

I am trying to do this on ASP.NET MVC 4: MemoryStream mem = new

  • 0

I am trying to do this on 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");

However the ABC.docx opens as corrupted and it wouldn’t open even after fixing it.

Any ideas?

Linked Qs:

Streaming In Memory Word Document using OpenXML SDK w/ASP.NET results in "corrupt" document

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:29:55+00:00Added an answer on June 15, 2026 at 9:29 pm

    Apparently the problem comes from missing this 2 lines:

    wordDoc.AddMainDocumentPart();
    wordDoc.MainDocumentPart.Document = doc;
    

    Updated the code to below and it now works flawlessly, even without any extra flushing, etc necessary.

    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");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Kinda new at this... I am using ASP.net MVC 1 and I am trying
I'm trying to create a sitemap in an ASP.NET MVC project. This code in
I am trying to do this tutorial http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/getting-started-with-mvc3-part4-cs but instead of using the compact
I'm trying to use the Facebook assemblies in ASP.NET MVC 3. I'm following this
I'm using ASP.Net with MVC 3 and trying to auto generate images. In my
I am trying to learn Asp.net Mvc so I am trying out this Tutorial.
I am using ASP.NET MVC 2 with nhibernate. I have this Sales class. Sales
I'm having this error trying to debug my ASP.NET MVC app. I've set the
I am trying to implement an autocomplete in ASP.NET MVC 3, following this post
I'm trying to teach myself .Net MVC 3, and am following this tutorial: http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/intro-to-aspnet-mvc-3

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.