Editing saving docx in asp net program using openxml memorystream


ASP.NET is a popular programming language used for developing web applications. It provides a for building dynamic websites and web . One common requirement in web development is the ability to edit and save documents, such as DOCX files. In this article, we will explore how to achieve this using the library and in ASP.NET.

Using OpenXML

OpenXML is a library that allows developers to manipulate documents . It provides a set of classes and methods for creating, editing, and saving documents in various formats, including DOCX. To use OpenXML in an ASP.NET application, you need to add a reference to the DocumentFormat.OpenXml assembly.

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

Editing and Saving DOCX

Let's say we have a web application that allows users to upload a DOCX file, make some changes, and save it back to the server. Here's an example of how you can achieve this using OpenXML and MemoryStream.

protected void btnSave_Click(object sender, EventArgs e)
    // Get the uploaded file
    HttpPostedFile uploadedFile = fileUploadControl.PostedFile;

    //  a MemoryStream to hold the file data
    using (MemoryStream memoryStream = new MemoryStream())
        // Copy the uploaded file to the MemoryStream

        // Open the document using OpenXML
        using (WordprocessingDocument document = WordprocessingDocument.Open(memoryStream, true))
            // Make the necessary changes to the document
            // For example, replace all occurrences of a specific text
            foreach (var text in document.MainDocumentPart.Document.Descendants())
                if (text.Text.Contains("oldText"))
                    text.Text = text.Text.Replace("oldText", "newText");

            // Save the changes

        // Save the  document to the server
        using (FileStream fileStream = new FileStream(Server.("modified.docx"), FileMode.Create))

In the above example, we first retrieve the uploaded file using the file upload . We then create a MemoryStream to hold the file data. The uploaded file is copied to the MemoryStream using the CopyTo method.

Next, we open the document using OpenXML's WordprocessingDocument class. We can then make the necessary changes to the document. In this example, we are replacing all occurrences of “oldText” with “newText” in the document.

After making the changes, we save the document using the Save method. Finally, we save the modified document to the server by creating a FileStream and writing the MemoryStream data to it.


In this article, we have explored how to edit and save DOCX files in an ASP.NET application using the OpenXML library and MemoryStream. By leveraging the power of OpenXML, developers can easily manipulate Office documents programmatically. This opens up a wide range of possibilities for creating dynamic and interactive web applications.

Rate this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Table of Contents