Generated package does not include certain required parts
See original GitHub issueDescription 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:
- Created 5 years ago
- Comments:19 (7 by maintainers)
Top GitHub Comments
@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.
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.