Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法为Excel设置Xml格式_C#_Xml_Excel_Xelement - Fatal编程技术网

C# 无法为Excel设置Xml格式

C# 无法为Excel设置Xml格式,c#,xml,excel,xelement,C#,Xml,Excel,Xelement,我正在尝试格式化一个XML文件,以便可以直接使用Microsoft Excel打开它 我遇到的问题是由于默认名称空间和别名ss:namespace是相同的。否则我无法让excel接受该文件 在尝试下面的代码se时,我在工作表节点上获得了一个额外的xmlns=“”属性。这使Excel无法打开该文件 XNamespace nsDefault = "urn:schemas-microsoft-com:office:spreadsheet"; XElement workbook = new XEleme

我正在尝试格式化一个XML文件,以便可以直接使用Microsoft Excel打开它

我遇到的问题是由于默认名称空间和别名ss:namespace是相同的。否则我无法让excel接受该文件

在尝试下面的代码se时,我在工作表节点上获得了一个额外的xmlns=“”属性。这使Excel无法打开该文件

XNamespace nsDefault = "urn:schemas-microsoft-com:office:spreadsheet";
XElement workbook = new XElement(nsDefault + "Workbook", 
    new XAttribute("xmlns", nsDefault),
new XAttribute(XNamespace.Xmlns + "ss", nsDefault));

XElement worksheet = new XElement("Worksheet");
worksheet.SetAttributeValue(nsDefault + "Name", "Shipping");

XElement table = new XElement("Table");

XElement row = new XElement("Row");
XElement cell = new XElement("Cell");
XElement data = new XElement("Data");
data.SetAttributeValue(nsDefault + "Type", "String");
data.Value = "qwerty";

cell.Add(data);
row.Add(cell);
table.Add(row);

worksheet.Add(table);
workbook.Add(worksheet);

workbook.Save(@".\Xml\Test.xml");

有人知道我如何解决这个问题吗?

您最好使用Microsoft提供的
OpenXMLSDK
。从以下位置获取有关它的更多信息:


你为什么要发明自己的轮子?您是否看过直接创建MS conform XML文件的OpenXML?()Excel 2010?还是旧版本?Excel 2010。我将研究openxml,谢谢