question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generated package does not include certain required parts

See original GitHub issue

Description Started on MSDN support first. https://social.msdn.microsoft.com/Forums/office/en-US/fa395b57-ab12-4fe8-9fa1-0726c18fe0cd/openxml-generated-file-wont-open-in-ms-spreadsheet-compare-tool-and-excel-quotopen-and?forum=oxmlsdk

The basic problem is when a new file is created by OpenXML, it appears to load in Excel, but if you use the Excel File Open tool for “Open and Repair” you can see there are some hidden errors being reported. My assumption is the tool Spreadhsheet Compare tool from MS Office can’t open this file because of teh same underlying error. If the file is opened and then saved in Excel, the tool now open the file.

Information

  • .NET Target: 4.7.1
  • DocumentFormat.OpenXml Version: 2.8.1.0

Repro

public static void CreateSpreadsheetWorkbook(string filepath)
        {
            // Create a spreadsheet document by supplying the filepath.
            // By default, AutoSave = true, Editable = true, and Type = xlsx.
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(filepath, SpreadsheetDocumentType.Workbook);

            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.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 = spreadsheetDocument.WorkbookPart.Workbook.
                AppendChild<Sheets>(new Sheets());

            // Append a new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.
                    GetIdOfPart(worksheetPart),
                SheetId = 1,
                Name = "mySheet"
            };
            sheets.Append(sheet);

            workbookpart.Workbook.Save();

            // Close the document.
            spreadsheetDocument.Close();
        }

Observed

Spreadsheet Comparison tool will report https://support.office.com/en-us/article/Basic-tasks-in-Spreadsheet-Compare-F2B20AF8-A6D3-4780-8011-F15B3229F5D8

“Error opening workbook. Attempted to read past the end of the stream”

If you attempt to open the file through Excel “Open and Repair” option, you will get this XML error.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<logFileName>error260080_01.xml</logFileName><summary>Errors were detected in file 'C:\TEMP\testing.xlsx'</summary>
<additionalInfo><info>Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.</info></additionalInfo>
</recoveryLog>

Expected File should open without errors.

Please add your expected behavior here.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:19 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tomjebocommented, Sep 25, 2018

@NerdBrick (@twsouthwick @tarunchopra) I spent some time today testing this scenario. I found that a blank, newly created Excel workbook (no SDK involved) behaves the same way when using the “Open and Repair” option in Excel. So I focused on the Spreadsheet Compare tool.

After whittling down the Excel genned file to the same structure and content as the SDK genned one (tediously), I found that the one thing that the Spreadsheet Compare tool didn’t like was the “x:” ns prefix in this line in the sheet.xml part:

<x:sheetData/>

The SDK uses a xmlns declaration attribute that specifies “x:” as the prefix like this:

<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">

Excel uses other prefixes but doesn’t on this first declaration as it is the default spreadsheetml/2006/main namespace.

The “x:sheetData” is being emitted as part of schema generated code and this would be a little tricky to change in the SDK, not to mention special case. Also, from everything I can discern, the XML emitted by the SDK is completely legal, the Spreadsheet Compare tool just doesn’t like it. I think this would be a bug in the tool. Our team can file a bug with the Excel tools folks.

0reactions
NerdBrickcommented, May 28, 2020

@NerdBrick, where you able to get an answer to this issue? I just recently took on helping resolve this issue in an application. The end user will not want to open and save every time an Excel file is generated.

When the file is first generated, I noticed the size of the application is small. When I open up and save the file in Excel, the file almost doubles in size with all of the corrections needed.

I’m sorry for the long delay replying. I no longer work for the company that I was using OpenXML for.

Sorry I wasn’t of more help.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why does IntelliJ give me "Package doesn't exist" error?
I'm trying to use the barbecue barcode printing library. I have successfully added the library to IntelliJ through project structure add library ...
Read more >
Troubleshooting NuGet Package Restore in Visual Studio
A description of common NuGet restore errors in Visual Studio and how to troubleshoot them.
Read more >
Package Codes
Package setup involves defining package elements, or codes, and collecting these elements into groups, or packages. A package element is defined as an ......
Read more >
Package Element Actions - Endevor® 18.0 - TechDocs
You create a package by first defining the package, and then including SCL in the package to specify element actions to be performed....
Read more >
7. Declaring relationships between packages
Some binary packages incorporate parts of other packages when built but do not have to depend on those packages. Examples include linking with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found