Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
MagnusBeijer opened this issue Aug 13, 2024 · 6 comments · Fixed by #1828
Closed

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

MagnusBeijer opened this issue Aug 13, 2024 · 6 comments · Fixed by #1828
Assignees

Comments

@MagnusBeijer
Copy link

MagnusBeijer commented Aug 13, 2024

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
@MagnusBeijer MagnusBeijer changed the title Calling Worksheet.Save() makes rest of changes not apply Calling Worksheet.Save() makes rest of changes to worksheet not apply in 3.1 Aug 13, 2024
@Ulrich-Personal
Copy link

Ulrich-Personal commented Aug 14, 2024

I am experiencing the same problem. After WorksheetPart.Worksheet.Save() has been executed following changes are not recognized despite calling WorksheetPart.Worksheet.Save() again...

I am not providing any code snippet since it would logically be identical with the one provided by @MagnusBeijer

I am using OpenXml version 3.1.0

@twsouthwick
Copy link
Member

Thanks for the report! I'll take a look

@twsouthwick twsouthwick self-assigned this Aug 20, 2024
@pharmaprofi
Copy link

Hello. I have a same problem.
Appen Row in SheetData not working after Document.Save()
Without any erros. File contais nunly rows each was added before Save()

Fixed after downgrade DocumentFormat.OpenXml.dll and DocumentFormat.OpenXml.Framework.dll from 3.1.0 to 3.0.2

public void AddRow()
{
    if (Row != null)
    {
        ShData.Append(Row);
    }

    Row = new Row();
    rowInd++;
    if (rowInd % 1000 == 0)
    {
        Document.Save();
    }

}

@Ulrich-Personal
Copy link

Is there any progress expected in the near future?

@nuelle16
Copy link

Is this going to be solved anytime soon? Do you have a rough estimate @twsouthwick ?

@twsouthwick
Copy link
Member

Found what happened - I made a change before 3.1 that if the part stream was retrieved, it would unload the existing root if data is written to it. However, in this case, the root is itself being written so it shouldn't be replaced. A fix is in #1828

twsouthwick added a commit that referenced this issue Nov 20, 2024
This is fall out from #1760 that removes the loaded part but doesn't
check if we're saving an existing root element

Fixes #1771
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants