1
votes

*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);
        }
2
What does /xl/worksheets/sheet2.xml look like?Gromer
I am not sure since the file created has errors.Kobojunkie
So, you can't open the created file?Gromer
The sample code at msdn.microsoft.com/en-us/library/office/ff478153.aspx makes it appear that your Workbook is missing the Sheets object that your Worksheet object should be associated withbarrowc

2 Answers

1
votes

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.

1
votes

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.. ;)