Skip to content

Calling Worksheet.Save() makes rest of changes to worksheet not apply in 3.1 #1771

@MagnusBeijer

Description

@MagnusBeijer

Describe the bug
When calling Worksheet.Save() in 3.0.2 changes were saved and you could continue to make changes to the worksheet.
In version 3.1 after calling Worksheet.Save() no other changes to the worksheet gets applied.

To Reproduce

using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

class Program
{
    static void Main(string[] args)
    {
        // Create a spreadsheet document
        using (SpreadsheetDocument doc = SpreadsheetDocument.Create(@"c:\temp\test.xlsx", SpreadsheetDocumentType.Workbook))
        {
            // Add a WorkbookPart to the document
            WorkbookPart workbookPart = doc.AddWorkbookPart();
            workbookPart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart
            WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
            worksheetPart.Worksheet = new Worksheet(new SheetData());

            // Add Sheets to the Workbook
            Sheets sheets = doc.WorkbookPart.Workbook.AppendChild(new Sheets());

            // Append a new sheet and associate it with the workbook
            Sheet sheet = new Sheet()
            {
                Id = doc.WorkbookPart.GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "Sheet1"
            };
            sheets.Append(sheet);
           
            // Get the SheetData from the Worksheet
            SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

            // Create a new row
            Row row = new Row() { RowIndex = 1 };
            sheetData.Append(row);
            worksheetPart.Worksheet.Save(); // Calling save here makes the document blank in 3.1

            // Create a new cell
            Cell cell = new Cell() { CellReference = "A1", CellValue = new CellValue("Hello"), DataType = CellValues.String };
            row.Append(cell);

            // Save the changes
            worksheetPart.Worksheet.Save();
        }
    }
}

Observed behavior
The worksheet is blank.

Expected behavior
A worksheet with one cell that says "Hello" is created.

Desktop

  • OS: Windows 11
  • Office version: Microsoft® Excel® for Microsoft 365 MSO (Version 2407 Build 16.0.17830.20056) 64-bit
  • .NET Target: .NET 8
  • DocumentFormat.OpenXml Version: 3.1

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions