C# 使用Linq to XML、XMLNS、XDeclaration和单元格格式创建Office Excel文档(works)

C# 使用Linq to XML、XMLNS、XDeclaration和单元格格式创建Office Excel文档(works),c#,.net,excel,linq-to-xml,ms-office,C#,.net,Excel,Linq To Xml,Ms Office,我需要将C#和Linq中的XML复制为XML。除了普通的.NET库之外,我不希望对其他库有任何依赖关系。XML如下所示 问题:我不知道如何打印这两行: <?mso-application progid="Excel.Sheet"?> <Data ss:Type="String">name</Data> 具有的第二行称为处理指令。其余的只是操纵名称空间 XNamespace ss = "urn:schemas-microsoft-com:office:spre

我需要将C#和Linq中的XML复制为XML。除了普通的.NET库之外,我不希望对其他库有任何依赖关系。XML如下所示

问题:我不知道如何打印这两行:

<?mso-application progid="Excel.Sheet"?>
<Data ss:Type="String">name</Data>
具有的第二行称为处理指令。其余的只是操纵名称空间

XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace x = "urn:schemas-microsoft-com:office:excel";
XNamespace x2 = "http://schemas.microsoft.com/office/excel/2003/xml";
XNamespace o = "urn:schemas-microsoft-com:office:office";
XNamespace html = "http://www.w3.org/TR/REC-html40";
XNamespace c = "urn:schemas-microsoft-com:office:component:spreadsheet";

XDocument doc = new XDocument(
        new XDeclaration("1.0", "UTF-8", string.Empty),
        new XComment(String.Format("Exported: {0}", DateTime.Now)),
        new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""),        
        new XElement("Workbook",            
            new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
            new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"),
            new XAttribute(XNamespace.Xmlns + "x2", "http://schemas.microsoft.com/office/excel/2003/xml"),
            new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet"),
            new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"),
            new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"),
            new XAttribute(XNamespace.Xmlns + "c", "urn:schemas-microsoft-com:office:component:spreadsheet"),
            new XElement("Worksheet", new XAttribute(ss + "Name", "Sheet 1"),               
                new XElement("Table",
                    new XElement("Row",
                        new XElement("Cell",
                            new XElement("Data", new XAttribute(ss + "Type", "String"),"status"))
                    )
                )
            )
        )
    );

使用上一个答案的输入完成问题。这只是为了向您展示如果将来有人搜索它,如何获得所需的结果。这是为了在Office Excel中打开而创建的。因此,Linq To XML导出为.XML文件,以便能够在Excel中轻松打开

目标格式:

<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">
  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office" />
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel" />
  <ss:Worksheet ss:Name="Sheet 1">
    <ss:Table>
      <ss:Row>
        <ss:Cell>
          <ss:Data ss:Type="String">name</ss:Data>
        </ss:Cell>
        <ss:Cell>
          <ss:Data ss:Type="String">sku</ss:Data>
        </ss:Cell>
      </ss:Row>
      <ss:Row>
        <ss:Cell>
          <ss:Data ss:Type="String">Suunto Elementum Terra</ss:Data>
        </ss:Cell>
        <ss:Cell>
          <ss:Data ss:Type="String">SS014522000</ss:Data>
        </ss:Cell>
      </ss:Row>
    </ss:Table>
  </ss:Worksheet>
</ss:Workbook>

@约翰·桑德斯-你能再解释一下吗@如果他需要控制前缀,那可能很重要。他几乎肯定不会,他需要的是正确的名称空间,而不是前缀。您的示例之所以有效,是因为所有元素都在默认名称空间中:您意识到了吗?@John Saunders-他说他需要“复制此XML”,这意味着“复制;复制”。在他的代码中,他使名称空间前缀与所需的XML匹配,因此我将其保留在那里,因为它是正确的。我的代码演示了如何添加处理指令,以及如何向其属性添加名称空间。也许你也应该发布一个答案,而不是只是投我的票。@TheJuice:如果我有时间,我会的。我不。顺便说一句,大多数认为必须完全复制XML的人并不理解XML名称空间。他们只需要复制名称空间,并且不知道所使用的前缀与任何自尊的程序无关。@TheJuice:在这种情况下,他应该承认这一点,这样我们就可以嘲笑他有一个不能正确理解XML名称空间的程序。我们可以帮他解决这个问题。但第一步是承认问题。:-)当我这样做时,我会注意到空白XMLNS——这会阻止Excel打开它。
XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace x = "urn:schemas-microsoft-com:office:excel";
XNamespace x2 = "http://schemas.microsoft.com/office/excel/2003/xml";
XNamespace o = "urn:schemas-microsoft-com:office:office";
XNamespace html = "http://www.w3.org/TR/REC-html40";
XNamespace c = "urn:schemas-microsoft-com:office:component:spreadsheet";

XDocument doc = new XDocument(
        new XDeclaration("1.0", "UTF-8", string.Empty),
        new XComment(String.Format("Exported: {0}", DateTime.Now)),
        new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""),        
        new XElement("Workbook",            
            new XAttribute(XNamespace.Xmlns + "xsi", "http://www.w3.org/2001/XMLSchema-instance"),
            new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"),
            new XAttribute(XNamespace.Xmlns + "x2", "http://schemas.microsoft.com/office/excel/2003/xml"),
            new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet"),
            new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"),
            new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"),
            new XAttribute(XNamespace.Xmlns + "c", "urn:schemas-microsoft-com:office:component:spreadsheet"),
            new XElement("Worksheet", new XAttribute(ss + "Name", "Sheet 1"),               
                new XElement("Table",
                    new XElement("Row",
                        new XElement("Cell",
                            new XElement("Data", new XAttribute(ss + "Type", "String"),"status"))
                    )
                )
            )
        )
    );
<?xml version="1.0" encoding="utf-8"?>
<?mso-application progid="Excel.Sheet"?>
<ss:Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:x2="http://schemas.microsoft.com/office/excel/2003/xml" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:html="http://www.w3.org/TR/REC-html40" xmlns:c="urn:schemas-microsoft-com:office:component:spreadsheet">
  <OfficeDocumentSettings xmlns="urn:schemas-microsoft-com:office:office" />
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel" />
  <ss:Worksheet ss:Name="Sheet 1">
    <ss:Table>
      <ss:Row>
        <ss:Cell>
          <ss:Data ss:Type="String">name</ss:Data>
        </ss:Cell>
        <ss:Cell>
          <ss:Data ss:Type="String">sku</ss:Data>
        </ss:Cell>
      </ss:Row>
      <ss:Row>
        <ss:Cell>
          <ss:Data ss:Type="String">Suunto Elementum Terra</ss:Data>
        </ss:Cell>
        <ss:Cell>
          <ss:Data ss:Type="String">SS014522000</ss:Data>
        </ss:Cell>
      </ss:Row>
    </ss:Table>
  </ss:Worksheet>
</ss:Workbook>
// Linq to XML - Namespaces
XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace x = "urn:schemas-microsoft-com:office:excel";
XNamespace x2 = "http://schemas.microsoft.com/office/excel/2003/xml";
XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
XNamespace o = "urn:schemas-microsoft-com:office:office";
XNamespace html = "http://www.w3.org/TR/REC-html40";
XNamespace c = "urn:schemas-microsoft-com:office:component:spreadsheet";

// Linq to XML - Document
XDocument doc = new XDocument(
    new XDeclaration("1.0", "UTF-8", string.Empty),
    new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""),
    new XElement(ns + "Workbook",
        new XAttribute("xmlns", ns.NamespaceName),
        new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
        new XAttribute(XNamespace.Xmlns + "x", x.NamespaceName),
        new XAttribute(XNamespace.Xmlns + "x2", x2.NamespaceName),
        new XAttribute(XNamespace.Xmlns + "ss", ss.NamespaceName),
        new XAttribute(XNamespace.Xmlns + "o", o.NamespaceName),
        new XAttribute(XNamespace.Xmlns + "html", html.NamespaceName),
        new XAttribute(XNamespace.Xmlns + "c", c.NamespaceName),
        new XElement(o + "OfficeDocumentSettings",
            new XAttribute("xmlns", o.NamespaceName)),
        new XElement(x + "ExcelWorkbook",
            new XAttribute("xmlns", x.NamespaceName)),
        new XElement("Worksheet",
            new XAttribute(ss + "Name", "Sheet 1"),
            new XElement("Table", // 1st Table
                new XElement("Row", // First Row
                    new XElement("Cell", // First Cell on First Row
                        new XElement("Data", new XAttribute(ss + "Type", "String"), "name") // Data in Cell A1
                    ),
                    new XElement("Cell",
                        new XElement("Data", new XAttribute(ss + "Type", "String"), "age") // Data in Cell B1
                    )
                )
            )
        )
    )
);
// Loop through a collection. Each iteration is a new row
foreach (Product product in products)
{
    // Linq to XML - Data
    doc.Descendants("Row").First().AddAfterSelf(
        new XElement("Row",
            new XElement("Cell",
                new XElement("Data", new XAttribute(ss + "Type", "String"), product.Name)), // Data in Cell A2
            new XElement("Cell",
                new XElement("Data", new XAttribute(ss + "Type", "String"), product.Age) // Data in Cell B2
            )
        )
    );
}
// Namespace fix. Deletes any empty xmlns="" text in every node.
foreach (XElement e in doc.Root.DescendantsAndSelf())
{ 
    if (e.Name.Namespace == string.Empty) 
    {
        e.Name = ns + e.Name.LocalName;
    } 
}