Unable to format Xml for Excel

↘锁芯ラ 提交于 2019-12-25 01:37:32

问题


im trying to format an XML-file so that its possible to open it directly using Microsoft Excel.

The problem im having is due to both the default namespace and alias ss: namespace is the same. I can't make excel accept the file otherwise.

When try the code, se below, I get an extra xmlns="" attribute on the WorkSheet node. This makes Excel unable to open the file.

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

Does anyone know how I can get around this?


回答1:


You're better off using the Open XML SDK that Microsoft provides. Get more information about it from here:

http://msdn.microsoft.com/en-us/library/office/hh180830.aspx

http://blogs.msdn.com/b/chrisquon/archive/2009/07/22/creating-an-excel-spreadsheet-from-scratch-using-openxml.aspx



来源:https://stackoverflow.com/questions/12075157/unable-to-format-xml-for-excel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!