Thanks to visit codestin.com
Credit goes to github.com

Skip to content

SpreadsheetDocument.Save() throws OpenXmlPackageException when saving .xlsb file due to unexpected styles.bin content type #1972

@koviroli

Description

@koviroli

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

  1. Open any .xlsb file with SpreadsheetDocument.Open(stream, true).
  2. Immediately call spreadsheetDocument.Save().
  3. 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 .xlsx and .xlsm.
  • The behavior cannot be reproduced with WordprocessingDocument or PresentationDocument for their respective formats.
  • The workaround (calling getters for all public properties on SpreadsheetDocument) suggests a lazy-loading/initialization or part registration issue specific to .xlsb styles content types.

Metadata

Metadata

Assignees

No one assigned

    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