Reputation: 6555
*strong text*I am trying to create and write to excel. For the template, I simply copied over code from OPENXML Productivity tool for an existing excel file, and then modified the code to add my rows of data when a new file is created from that code. However, I seem to be having some issue getting this to work correctly. I keep getting a
ccannot open the file : part /xl/worksheets/sheet2: Root element is missing Replaced Part: /xl/worksheets/sheet2.xml part with XML error. A document must contain exactly one root element. Line 1, column 0.
error which I am still unable to correctly debug. Also, in the original template, there were 3 sheets in my workbook. I made sure to delete two of them so I can have just the code for the one created, as I assumed the OpenXML 2.0 Productivity tool would only generate code for the sheet that existed at the time document code was put together. But what happens is that the generated file has all 4 of the original sheets in place, but seems to not be able to put together the headers on each.
Wanted to get some hints as to where to go to find this problem and fix it. thanks in advance.
public void CreatePackage(string filePath)
{
using (SpreadsheetDocument package = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
{
CreateParts(package);
}
}
private void CreateParts(SpreadsheetDocument document)
{
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId3");
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId3");
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId2");
GenerateThemePart1Content(themePart1);
WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
GenerateWorksheetPart1Content(worksheetPart1);
WorksheetCommentsPart worksheetCommentsPart1 = worksheetPart1.AddNewPart<WorksheetCommentsPart>("rId2");
GenerateWorksheetCommentsPart1Content(worksheetCommentsPart1);
VmlDrawingPart vmlDrawingPart1 = worksheetPart1.AddNewPart<VmlDrawingPart>("rId1");
GenerateVmlDrawingPart1Content(vmlDrawingPart1);
SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId4");
GenerateSharedStringTablePart1Content(sharedStringTablePart1);
SetPackageProperties(document);
}
Upvotes: 1
Views: 16837
Reputation: 9303
Just an FYI for anyone else who gets this necrothread in their Google results...
This error also occurs if you have an XML file with no actual data in it. Excel will report this as if you have too many roots, when the actual problem is none at all.
Upvotes: 0
Reputation: 4102
If I had to guess -- this error is cause by the fact that you removed the
WorksheetPart worksheetPart2 = workbookPart2.AddNewPart<WorksheetPart>("rId2");
GenerateWorksheetPart2Content(worksheetPart2);
line from the example. Note, I took your code and replaced the 1's with 2's..
Where you would look is in the
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1);
part. In there is a place where they had multiple sheet generation (not worksheet generation, which is the part you removed)
Sheets sheets1 = new Sheets();
Sheet sheet1 = new Sheet() { Name = "sheet1", SheetId = (UInt32Value)1U, Id = "rId1" };
Sheet sheet2 = new Sheet() { Name = "sheet2", SheetId = (UInt32Value)2U, Id = "rId2" };
sheets1.Append(sheet1);
sheets1.Append(sheet2);
That id in the Sheet initializer is actually a reference to the ID of the workbook. Since you got rid of the GenerateWorkbookPart2Content function, that reference is an error.
Ahh, the problem with soft references...
In Microsoft's example code -- they use:
sheet1.Id = workbookPart.GetIdOfPart(worksheetPart1);
Which is still a soft reference, but at least no hard coding.. ;)
Upvotes: 1
Reputation: 22989
The problem is in the way you are structuring the XML; as the error explains, XMLs must have exactly one root element. Let me give you some examples:
Valid XML document:
<users>
<user>
<name>Omar</name>
<age>25</age>
</user>
<user>
<name>Gabbo</name>
<age>41</age>
</user>
</users>
Invalid XML document:
<user>
<name>Omar</name>
<age>25</age>
</user>
<user>
<name>Gabbo</name>
<age>41</age>
</user>
In the first example, every element in the document is a child of users, while in the second one there are two users floating around.
Upvotes: 1