Search code examples
c#excelopenxmloffice-interopopenxml-sdk

Do you have to save the Open XML document after working on it?


Sorry, here's a stupid question. I've been working on a document using the Open XML SDK and I don't see the changes I've made to it after I dispose off the SpreadsheetDocument object.

using (var spreadsheet = SpreadsheetDocument.Open(file, true))
{
    // do stuff to the document
}


// when I exit the program and open the workbook as an end-user
// I don't see any of the changes I made

I think I used to see the changes with examples I did before but I can't be so sure.

Do you have to save the document after working on it to see the changes? I don't see such a method to save the document.


Solution

  • Sorry, a little bit of searching revealed to me what I'd forgotten. Apparently, you have to save the XML DOM you just updated.

    Since I updated the WorksheetPart, and the WorksheetPart is of type OpenXmlPart and its DOM is represented by the Worksheet property of type Worksheet, I had to do this:

    using (var spreadsheet = SpreadsheetDocument.Open(file, true))
    {
        // do stuff to the document
    
        // finally
        worksheetPart.Worksheet.Save();
    }