OpenXML 生成一个空的 SheetData 元素
OpenXML generates an empty SheetData element
所以我需要将一些数据从我的应用程序导出到一个 xml 文件中,我正在尝试通过使用 OpenXML 来做到这一点,但是我使用的代码生成了附加的(封闭的)元素并且它会阻止文件被打开。
<?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") + $@"\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" 元素是从哪里来的?光看是看不出来的
我真傻。从字面上看,在我发布问题后一秒钟,我注意到我在哪里创建了一个不必要的 sheetdata 元素
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData()); // HERE
所以我需要将一些数据从我的应用程序导出到一个 xml 文件中,我正在尝试通过使用 OpenXML 来做到这一点,但是我使用的代码生成了附加的(封闭的)元素并且它会阻止文件被打开。
<?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") + $@"\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" 元素是从哪里来的?光看是看不出来的
我真傻。从字面上看,在我发布问题后一秒钟,我注意到我在哪里创建了一个不必要的 sheetdata 元素
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData()); // HERE