1
所以我需要從我的應用程序導出一些數據到一個XML文件,我試圖通過使用OpenXML,但我使用的代碼生成aditional(封閉)元素和它會阻止文件被打開。OpenXML生成一個空的SheetData元素
<?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>
這是我用來生成文件的代碼。
SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(System.IO.Path.Combine(currentPath, "Export Data") + [email protected]"\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();
誰能告訴我額外的「SheetData」元素來自哪裏?我無法看清楚。
非常感謝!我做了完全相同的事情,看不到它是如何創建的。這爲我修好了。 – dodegaard