用C#编写Excel 2003

我想写一个2003 XML的Excel文件,但也许我错过了一些大的东西,或者…我根本不明白命名空间。

我需要写这个:

<?xml version="1.0" encoding="utf-8"?> <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"> <Worksheet Name="Questions"> <Table><Row><Cell><Data ss:Type="Number">1</Data></Cell></row></table> </Worksheet> 

我正在使用这样的代码:

  var toret = new XmlDocument(); // Add encoding declaration XmlDeclaration xmlDeclaration = toret.CreateXmlDeclaration( "1.0", "utf-8", null); toret.AppendChild( xmlDeclaration ); // Add root label var root = toret.CreateElement( ExcelXmlLblWorkbook ); toret.AppendChild( root ); // root.SetAttribute( "xmlns", "urn:schemas-microsoft-com:office:spreadsheet" ); root.SetAttribute( "xmlns:o", "urn:schemas-microsoft-com:office:office" ); root.SetAttribute( "xmlns:x", "urn:schemas-microsoft-com:office:excel" ); root.SetAttribute( "xmlns:ss", "urn:schemas-microsoft-com:office:spreadsheet" ); root.SetAttribute( "xmlns:html", "http://www.w3.org/TR/REC-html40" ); // ... var cellForQuestionNumber = wsQuestions.OwnerDocument.CreateElement( ExcelXmlLblCell ); row.AppendChild( cellForQuestionNumber ); var dataForQuestionNumber = wsQuestions.OwnerDocument.CreateElement( ExcelXmlLblData ); dataForQuestionNumber.SetAttribute( "ss:Type", "Number" ); dataForQuestionNumber.InnerText = "1"; cellForQuestionNumber.AppendChild( dataForQuestionNumber ); 
  1. 如果我尝试创build具有xmlns =“…”属性的根节点,那么在运行时会失败,说“命名空间名称无效”。

  2. 在Data节点中,不是像<Data ss:Type="Number">那样得到一个节点,而是得到了<Data d6p1:ss="String" xmlns:d6p1="Type"> ,我不太明白。 我也尝试过:

    dataForQuestionNumber.SetAttribute(“ss”,“Type”,“Number”);

但似乎并不奏效。