使用XDocument生成XML Excel文档

时间:2016-01-18 21:19:03

标签: c# xml excel xelement

我正在尝试生成像这样的XML Excel文档

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:o="urn:schemas-microsoft-com:office:office"
 xmlns:x="urn:schemas-microsoft-com:office:excel"
 xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
 xmlns:html="http://www.w3.org/TR/REC-html40">
 <Styles>
  <Style ss:ID="Default" /></Style>
 </Styles>
 <Worksheet ss:Name="Worksheet Name">
  <Table>
   <Row ss:AutoFitHeight="0">
    <Cell>
     <Data ss:Type="String">Test</Data>
    </Cell>
   </Row>
  </Table>
 </Worksheet>
</Workbook>

我目前的结果如下:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<ss:Workbook 
  xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:html="http://www.w3.org/TR/REC-html40">
  <Styles xmlns="">
    <Style ss:ID="Default" />
  </Styles>
  <Worksheet ss:Name="Worksheet Name" xmlns="">
    <Table>
      <Row ss:AutoFitHeight="0">
        <Cell>
          <Data ss:Type="String">Test</Data>
        </Cell>
      </Row>
    </Table>
  </Worksheet>
</ss:Workbook>

我尝试过很多不同的解决方案而没有任何运气。我需要用工作簿替换ss:Workbook并去除所有xmlns =&#34;&#34;。

我目前的代码看起来像这样(LinqPad):

void Main()
{
    XNamespace ss = "urn:schemas-microsoft-com:office:spreadsheet";
    XNamespace ns = "urn:schemas-microsoft-com:office:spreadsheet";

    var workbook = new XElement(ns + "Workbook");
    workbook.Add(new XAttribute("xmlns", "urn:schemas-microsoft-com:office:spreadsheet"));
    workbook.Add(new XAttribute(XNamespace.Xmlns + "o", "urn:schemas-microsoft-com:office:office"));
    workbook.Add(new XAttribute(XNamespace.Xmlns + "x", "urn:schemas-microsoft-com:office:excel"));
    workbook.Add(new XAttribute(XNamespace.Xmlns + "ss", "urn:schemas-microsoft-com:office:spreadsheet"));
    workbook.Add(new XAttribute(XNamespace.Xmlns + "html", "http://www.w3.org/TR/REC-html40"));

    var styles = new XElement("Styles");
    workbook.Add(styles);
    var style = new XElement("Style");
    style.Add(new XAttribute(ss + "ID", "Default"));
    styles.Add(style);

    var worksheet = new XElement("Worksheet");
    worksheet.Add(new XAttribute(ss + "Name", "Worksheet Name"));
    workbook.Add(worksheet);

    var table = new XElement("Table");
    worksheet.Add(table);

    var row = new XElement("Row");
    row.Add(new XAttribute(ss + "AutoFitHeight", 0));
    table.Add(row);

    var cell = new XElement("Cell");
    var data = new XElement("Data");
    data.Add(new XAttribute(ss + "Type", "String"));
    data.Value = "Test";
    cell.Add(data);
    row.Add(cell);

    var document = new XDocument(new XDeclaration("1.0", "", null));
    document.Add(new XProcessingInstruction("mso-application", "progid=\"Excel.Sheet\""));
    document.Add(workbook);

    var sw = new CustomStringWriter();
    document.Save(sw);
    sw.ToString().Dump();
    //sw.ToString().Replace("ss:Workbook", "Workbook").Replace("xmlns=\"\"", "").Dump();
}

// Define other methods and classes here

class CustomStringWriter : StringWriter
{
    public override Encoding Encoding
    {
        get
        {
            return null;
        }
    }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您的问题很可能是您没有理解默认命名空间是从父元素继承的。以此XML为例:

<parent xmlns="http://namespace.com/uri">
    <child />
</parent>

在此,child也有名称空间http://namespace.com/uri

因此,您StyleWorksheet元素等都有xmlns=""的原因是因为您明确表示这些元素没有命名空间。

您应该修改所有这些元素的创建,以包含正确的命名空间,例如:

var styles = new XElement(ss + "Styles");

顺便说一下,我还要注意LINQ to XML支持比你目前采用的更具声明性的方法。您可以通过构造函数传递所有元素的内容。所以你可以改写你的风格:

var styles = new XElement(ss + "Styles",
    new XElement(ss + "Style",
        new XAttribute("ID", "Default")
        )
    );

请参阅this fiddle了解您的固定样本&amp;用这种风格重写。