dotnet / Open-XML-SDK

Open XML SDK by Microsoft
https://www.nuget.org/packages/DocumentFormat.OpenXml/
MIT License
3.99k stars 544 forks source link

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

Open MagnusBeijer opened 1 month ago

MagnusBeijer commented 1 month ago

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

Ulrich-Personal commented 4 weeks ago

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 commented 3 weeks ago

Thanks for the report! I'll take a look

pharmaprofi commented 2 weeks ago

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();
     }

 }