Reputation: 197
So i need to export some data from my application into an xml file and i'm trying to do that by using OpenXML, but the code that i'm using generates aditional (closed) element and it prevents the file from being opened.
<?xml version="1.0" encoding="utf-8"?>
<x:worksheet xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:sheetData /> <---- this guy here.
<x:sheetData>
<x:row><x:c t="str">
<x:v>Key</x:v></x:c>
<x:c t="str"><x:v>Value</x:v></x:c>
</x:row>
</x:sheetData>
</x:worksheet>
And this is the code that i use to generate the file.
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(System.IO.Path.Combine(currentPath, "Export Data") + $@"\Export-{DateTime.Now.Year}.xlsx", SpreadsheetDocumentType.Workbook);
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Temp"
};
sheets.Append(sheet);
SheetData sheetData = worksheetPart.Worksheet.AppendChild(new SheetData());
Row row = new Row();
row.Append(
ConstructCell("Key", CellValues.String),
ConstructCell("Value", CellValues.String));
sheetData.AppendChild(row);
worksheetPart.Worksheet.Save();
spreadsheetDocument.Close();
Can anyone tell me where the extra "SheetData" element is coming from? I can't tell just by looking.
Upvotes: 1
Views: 1065
Reputation: 197
I'm so stupid. Literally a second after i posted the question i noticed where i'm creating a unnecessary sheetdata element
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData()); // HERE
Upvotes: 1