Open XML worksheet insert - "... unreadable content ..."

When I follow this guide:

http://msdn.microsoft.com/en-us/library/cc881781.aspx

to open an Excel document and insert a blank sheet, the end result will be a message in which "Excel found unreadable content in ... You want to restore the contents of this book ...". If I restore all inserted sheets, they will be empty (even if I add content programmatically)

After renaming xlsx to .zip and checking it, it shows that worksheets have been created and content has been added.

Anyone who has similar problems? It may be something that does not create a relationship between the newly created parts ...

+4
source share
1 answer

This error message means that the XML that makes up your excel document does not match the XML schema and is invalid. You can use the Open XML SDK 2.0 Productivity Tool to find out where the problem is.

I also copied the code from the bottom of your link and made it work, as Chris said in his comment. Does your code look like this?

// Open the document for editing. using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) { // Add a blank WorksheetPart. WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>(); newWorksheetPart.Worksheet = new Worksheet(new SheetData()); Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>(); string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart); // Get a unique ID for the new worksheet. uint sheetId = 1; if (sheets.Elements<Sheet>().Count() > 0) { sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1; } // Give the new worksheet a name. string sheetName = "Sheet" + sheetId; // Append the new worksheet and associate it with the workbook. Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; sheets.Append(sheet); string docName = @"C:\Users\Public\Documents\Sheet7.xlsx"; InsertWorksheet(docName); } // Given a document name, inserts a new worksheet. public static void InsertWorksheet(string docName) { // Open the document for editing. using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) { // Add a blank WorksheetPart. WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>(); newWorksheetPart.Worksheet = new Worksheet(new SheetData()); Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>(); string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart); // Get a unique ID for the new worksheet. uint sheetId = 1; if (sheets.Elements<Sheet>().Count() > 0) { sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1; } // Give the new worksheet a name. string sheetName = "Sheet" + sheetId; // Append the new worksheet and associate it with the workbook. Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; sheets.Append(sheet); } } 
+7
source

All Articles