-
Notifications
You must be signed in to change notification settings - Fork 572
Description
Describe the bug
Saving an .xlsb opened via SpreadsheetDocument throws an OpenXmlPackageException about an unexpected content type for /xl/styles.bin. This occurs even if no modifications are made; simply opening and calling Save() triggers the error for .xlsb. The same code path works for .xlsx and .xlsm.
Exception
DocumentFormat.OpenXml.Packaging.OpenXmlPackageException: 'The document cannot be opened because there is an invalid part with an unexpected content type.
[Part Uri=/xl/styles.bin],
[Content Type=application/vnd.ms-excel.styles],
[Expected Content Type=application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml].'
A temporary workaround is to force-get all public properties of the SpreadsheetDocument instance before saving; after doing so, Save() succeeds.
Screenshots
N/A
To Reproduce
A self-contained, minimal repro (console app):
using DocumentFormat.OpenXml.Packaging;
var filePath = @"PathToXlsb\Book1.xlsb";
var fileBytes = File.ReadAllBytes(filePath);
var stream = new MemoryStream();
stream.Write(fileBytes, 0, fileBytes.Length);
stream.Position = 0;
using var spreadsheetDocument = SpreadsheetDocument.Open(stream, true);
// works with this solution
//foreach (var prop in spreadsheetDocument.GetType().GetProperties())
//{
// try
// {
// _ = prop.GetValue(spreadsheetDocument);
// }
// catch { }
//}
// Throws exception
spreadsheetDocument.Save();
Steps
- Open any
.xlsbfile withSpreadsheetDocument.Open(stream, true). - Immediately call
spreadsheetDocument.Save(). - Observe the
OpenXmlPackageException.
Observed behavior
Accessing all public properties on SpreadsheetDocument prior to saving (reflection-based getter loop) makes the save succeed.
Expected behavior
Opening a valid .xlsb file, optionally modifying custom properties, and calling Save() should succeed without requiring any reflection-based workarounds.
Desktop (please complete the following information)
- OS: Windows 11 Home (26100.2033)
- Office version: N/A (repro does not require Office; opening/saving via SDK)
- .NET Target: .NET 8 and .NET 10 (both reproduce)
- DocumentFormat.OpenXml Version(s) tested: 3.3.0, 3.2.0, 3.1.1, 3.0.2, 3.0.1
Additional context
- Only happens with
.xlsb. The same code works for.xlsxand.xlsm. - The behavior cannot be reproduced with
WordprocessingDocumentorPresentationDocumentfor their respective formats. - The workaround (calling getters for all public properties on
SpreadsheetDocument) suggests a lazy-loading/initialization or part registration issue specific to.xlsbstyles content types.