1

I'm trying to write a 2003 XML Excel file, but maybe I'm missing something big, or... I simply don't understand namespaces.

I need to write this:

<?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>

And I am using code like this one:

    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. If I try to create the root node with the attribute for the xmlns="..." then it fails at runtime saying that "the namespace name is invalid".

  2. In the Data nodes, instead of getting a node like <Data ss:Type="Number">, I get <Data d6p1:ss="String" xmlns:d6p1="Type">, which I don't really understand. I've also tried with:

    dataForQuestionNumber.SetAttribute( "ss", "Type", "Number" );

But it does not seems to work.

Baltasarq
  • 12,014
  • 3
  • 38
  • 57
  • Checkout this precious Stackoverflow post and look at the DataSet example , this may be easier to understand. http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp – MethodMan May 31 '12 at 17:07
  • Thank you @DJ KRAZE, but this format is very very simple, I am not really interested in third party libraries. Also, now I'm really curious to know what am I doing wrong. – Baltasarq May 31 '12 at 18:03
  • Thats not Third Party by the way comes with .NET – MethodMan May 31 '12 at 18:08
  • I honestly don't know which question is part with .NET. Do you mean I should install Excel and automate it from a COM object? I cannot do that, I'm using C#/mono_2.8. – Baltasarq Jun 01 '12 at 11:21
  • You should look at using XDocument if it is available in mono. – Chuck Savage Jun 05 '12 at 10:07

0 Answers0