Calling Worksheet.Save() makes rest of changes to worksheet not apply in 3.1
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
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
Thanks for the report! I'll take a look
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();
}
}
Is there any progress expected in the near future?
Is this going to be solved anytime soon? Do you have a rough estimate @twsouthwick ?
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