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

A basic OpenXml created Workbook breaks in 'Spreadsheet Compare' #1789

Closed
TrevorT opened this issue Sep 11, 2024 · 2 comments
Closed

A basic OpenXml created Workbook breaks in 'Spreadsheet Compare' #1789

TrevorT opened this issue Sep 11, 2024 · 2 comments
Assignees

Comments

@TrevorT
Copy link

TrevorT commented Sep 11, 2024

Describe the bug
I came across this in a more complicated scenario (creating a SpreadsheetDocument from an existing stream, modifying the doc and streaming it out to another different file), but I've distilled the issue to the below example.
In my test the Excel file is created and valid, but trying to use it in "Spreadsheet Compare" causes that app to crash with a "Attempt to read past end of stream" exception.
Opening the generated file in Excel, and saving it again (unchanged) fixes the issue - so the save from within Excel is modifying (fixing) the document.

To Reproduce
Use the following test to create the workbook:

        [Fact]
        public void CreateAndSaveSpreadsheet()
        {
            // Arrange
            string outputFilename = Path.Combine(Path.GetDirectoryName(GetType().Assembly.Location)!, "OutputFolder", "Blank Document Created.xlsx");

            if (File.Exists(outputFilename))
                File.Delete(outputFilename);

            // Act
            using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Create(outputFilename, SpreadsheetDocumentType.Workbook))
            {
                WorkbookPart workbookPart = spreadsheetDoc.AddWorkbookPart();
                workbookPart.Workbook = new Workbook();

                WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
                worksheetPart.Worksheet = new Worksheet(new SheetData());

                Sheets sheets = spreadsheetDoc.WorkbookPart.Workbook.AppendChild(new Sheets());

                Sheet sheet = new Sheet()
                              {
                                  Id = spreadsheetDoc.WorkbookPart.GetIdOfPart(worksheetPart),
                                  SheetId = 1,
                                  Name = "Sheet1"
                              };
                sheets.Append(sheet);

                // Save the Workbook
                workbookPart.Workbook.Save();
                spreadsheetDoc.Save();
            }
        }

Steps to reproduce the behavior:

  1. Run the test to create the (basically empty) Workbook.
  2. Run Spreadsheet Compare. Choose the above created file for one of the comparison files
  3. See error:
    image
  4. To reproduce a 'fix': Open the created doc in Excel, save it unchanged, redo the Spreadsheet compare (no exception occurs)

Observed behavior
Unexpected exception.

Expected behavior
No exception from spreadsheet compare.
No significant internal file changes when an OpenXml created Workbook is opened in Excel and saved unchanged.

Desktop (please complete the following information):

  • OS: Windows 10
  • Office version: Office 365:
    image
  • .NET Target: .Net 6.0
  • DocumentFormat.OpenXml Version: 2.20.0

Additional context
Some investigation has led me to believe that the presence of the xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
entry for the default namespace, and the corresponding x: namespace prefixes to the xml tags is the issue.
image

If I manually edit the internals of the xlsx (sheet1.xml ) file and change the default namespace to have no alias in the 'sheet1.xml' file (as below) the issue is fixed (Spreadsheet Compare does not fail).
image

I have found to no way to get OpenXml to remove the x alias for the default namespace declared in the document XML.

@JarRami
Copy link

JarRami commented Oct 23, 2024

Seems this was reported earlier atleast at #509 but a fix was never reached? I'm running into this through ClosedXML (for.ex. ClosedXML/ClosedXML#1436) and would be nice to have it fixed.

@twsouthwick
Copy link
Member

This is valid xml and conforms to the spec. The issue lies in Spreadsheet Compare for some reason not correctly handling xml namespaces.

I'm going to close this issue, but if you want to provide an opt-in way to control the namespaces, feel free to reopen and submit a PR.

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

No branches or pull requests

3 participants