Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

Problem using XmlTextWriter export to excel

$
0
0

Hi everybody,

   Having problem getting the desired result of xml file. Can you guys help me with this? The function is exporting data from database to an excel file, and then importing the changes in
data back to database. Thanks in advanced.


Current Code:
[code]

const string sheet = "urn:schemas-microsoft-com:office:spreadsheet", office = "urn:schemas-microsoft-com:office:office", excel = "urn:schemas-microsoft-com:office:excel";
     XmlTextWriter xmlTextWriter = new XmlTextWriter(filePath, System.Text.Encoding.Default);
     xmlTextWriter.Formatting = Formatting.Indented;
     xmlTextWriter.WriteStartDocument();
     xmlTextWriter.WriteProcessingInstruction("mso-application", "progid=\"Excel.Sheet\"");
     xmlTextWriter.WriteStartElement(string.Empty, "Workbook", sheet);
     xmlTextWriter.WriteAttributeString("xmlns", "o", "http://www.w3.org/2000/xmlns/", office);
     xmlTextWriter.WriteAttributeString("xmlns", "x", "http://www.w3.org/2000/xmlns/", excel);

     xmlTextWriter.WriteStartElement(string.Empty, "DocumentProperties", office);
     xmlTextWriter.WriteElementString("Author", "Somebody");
     xmlTextWriter.WriteEndElement();//End DocumentProperties

     xmlTextWriter.WriteStartElement(string.Empty, "ExcelWorkbook", excel);     
     xmlTextWriter.WriteElementString("WindowHeight", "10230");
     xmlTextWriter.WriteElementString("WindowWidth", "13275");
     xmlTextWriter.WriteElementString("WindowTopX", "480");
     xmlTextWriter.WriteElementString("WindowTopY", "75");
     xmlTextWriter.WriteElementString("ProtectStructure", "False");
     xmlTextWriter.WriteElementString("ProtectWindows", "False");
     xmlTextWriter.WriteEndElement();//End ExcelWorkbook

     xmlTextWriter.WriteStartElement("Styles", sheet);
     xmlTextWriter.WriteStartElement(string.Empty, "Style", sheet);
     xmlTextWriter.WriteAttributeString("ID", sheet, "Default");
     xmlTextWriter.WriteAttributeString("Name", "ss", "Normal");
     xmlTextWriter.WriteStartElement("Alignment", sheet);     
     xmlTextWriter.WriteAttributeString("Vertical", "ss", "Bottom");
     xmlTextWriter.WriteEndElement();//End Alignment
     xmlTextWriter.WriteStartElement("Font", sheet);
     xmlTextWriter.WriteEndElement();//End Font
     xmlTextWriter.WriteStartElement("Interior", sheet);
     xmlTextWriter.WriteEndElement();//End Interior
     xmlTextWriter.WriteStartElement("NumberFormat", sheet);
     xmlTextWriter.WriteEndElement();//End NumberFormat
     xmlTextWriter.WriteStartElement("Protection", sheet);
     xmlTextWriter.WriteEndElement();//End Protection    
     xmlTextWriter.WriteEndElement();//End Style
     xmlTextWriter.WriteEndElement();//End Styles

     xmlTextWriter.WriteStartElement(string.Empty,"WorkSheet", sheet);
     xmlTextWriter.WriteAttributeString(string.Empty,"Name", sheet, "Sheet1");

     xmlTextWriter.WriteStartElement(string.Empty, "Table", sheet);
     xmlTextWriter.WriteAttributeString("ExpandedColumnCount", "1");
     xmlTextWriter.WriteAttributeString("ExpandedRowcount", "1");
     xmlTextWriter.WriteAttributeString("TotalColumns", "3");
     xmlTextWriter.WriteAttributeString("TotalRows", "1");//To be modified

     //xmlTextWriter.WriteStartElement(string.Empty, "Column", sheet);
     //xmlTextWriter.WriteAttributeString("ss", "Index", sheet, "1");
     //xmlTextWriter.WriteAttributeString("ss", "AutoFitWidth", sheet, "0");
     //xmlTextWriter.WriteAttributeString("ss", "Width", sheet, "82.5");
     //xmlTextWriter.WriteEndElement();//End Column    

     xmlTextWriter.WriteStartElement("Row", sheet);
     
     xmlTextWriter.WriteStartElement("Cell", sheet);
     //xmlTextWriter.WriteStartElement(string.Empty, "Data", sheet);     
     //xmlTextWriter.WriteElementString("Data","ID");
     xmlTextWriter.WriteRaw("<Data ss:Type=\"String\">ID</Data>");
     //xmlTextWriter.WriteAttributeString("ss", "Type", sheet, "String");
     //xmlTextWriter.WriteEndElement();//End Data    
     xmlTextWriter.WriteEndElement();//End Cell 

     xmlTextWriter.WriteStartElement("Cell", sheet);
     //xmlTextWriter.WriteStartElement(string.Empty, "Data", sheet);
     //xmlTextWriter.WriteElementString("Data", "Name");
     xmlTextWriter.WriteRaw("<Data ss:Type=\"String\">Name</Data>");
     //xmlTextWriter.WriteAttributeString("ss", "Type", sheet, "String");
     //xmlTextWriter.WriteEndElement();//End Data    
     xmlTextWriter.WriteEndElement();//End Cell   

     xmlTextWriter.WriteEndElement();//End Row    

     xmlTextWriter.WriteStartElement("Row", sheet);
     xmlTextWriter.WriteStartElement("Cell", sheet);
     //xmlTextWriter.WriteStartElement(string.Empty, "Data", sheet);
     //xmlTextWriter.WriteElementString(string.Empty, "Data", excel, "1");
     xmlTextWriter.WriteRaw("<Data ss:Type=\"Number\">1</Data>");
     //xmlTextWriter.WriteAttributeString("ss", "Type", sheet, "Number");
     //xmlTextWriter.WriteEndElement();//End Data    
     xmlTextWriter.WriteEndElement();//End Cell 
     xmlTextWriter.WriteStartElement("Cell", sheet);
     //xmlTextWriter.WriteStartElement(string.Empty, "Data", sheet);
     //xmlTextWriter.WriteElementString(string.Empty, "Data", excel, "Sample");
     xmlTextWriter.WriteRaw("<Data ss:Type=\"String\">Sample</Data>");
     //xmlTextWriter.WriteAttributeString("ss", "Type", sheet, "String");
     //xmlTextWriter.WriteEndElement();//End Data    
     xmlTextWriter.WriteEndElement();//End Cell   
     xmlTextWriter.WriteEndElement();//End Row    

 

     xmlTextWriter.WriteEndElement();//End Table    

     xmlTextWriter.WriteStartElement(string.Empty, "WorksheetOptions", excel);
     xmlTextWriter.WriteStartElement("Selected", excel);
     xmlTextWriter.WriteEndElement();//End Selected    
     xmlTextWriter.WriteStartElement("Panes", excel);
     xmlTextWriter.WriteStartElement("Pane", excel);     
     xmlTextWriter.WriteElementString("Number", "3");//To be modified
     xmlTextWriter.WriteElementString("ActiveRow", "3");//To be modified
     xmlTextWriter.WriteElementString("ActiveCol", "3");//To be modified

     xmlTextWriter.WriteEndElement();//End Pane    
     xmlTextWriter.WriteEndElement();//End Panes    
     xmlTextWriter.WriteElementString("ProtectObjects", "False");//To be modified
     xmlTextWriter.WriteElementString("ProtectScenarios", "False");//To be modified
     xmlTextWriter.WriteEndElement();//End WorksheetOptions    
     xmlTextWriter.WriteEndElement();//End WorkSheet 
     xmlTextWriter.WriteEndElement();//End Workbook

     xmlTextWriter.WriteEndDocument();
     xmlTextWriter.Close();
[code]

 

Desired XML Result:

<?xml version="1.0"?>
<?mso-application progid='Excel.Sheet'?>
<Workbook xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"> 
<Worksheet ss:Name="Sheet1">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
  <Version>11.8107</Version>
 </DocumentProperties>
 <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
  <WindowHeight>10005</WindowHeight>
  <WindowWidth>10005</WindowWidth>
  <WindowTopX>120</WindowTopX>
  <WindowTopY>135</WindowTopY>
  <ProtectStructure>False</ProtectStructure>
  <ProtectWindows>False</ProtectWindows>
 </ExcelWorkbook>
 <Styles>
  <Style ss:ID="Default" ss:Name="Normal">
   <Alignment ss:Vertical="Bottom"/>
   <Borders/>
   <Font/>
   <Interior/>
   <NumberFormat/>
   <Protection/>
  </Style>
 </Styles>
  <Table ss:ExpandedColumnCount="4" ss:ExpandedRowCount="4" x:FullColumns="1"
   x:FullRows="1">
   <Column ss:Index="3" ss:AutoFitWidth="0" ss:Width="82.5"/>
   <Row>
    <Cell><Data ss:Type="String">ID</Data></Cell>
    <Cell><Data ss:Type="String">Name</Data></Cell>
    <Cell><Data ss:Type="String">Name in Chinese</Data></Cell>
    <Cell><Data ss:Type="String">Name in Chinese</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="Number">1</Data></Cell>
    <Cell><Data ss:Type="String">gg</Data></Cell>
    <Cell ss:Index="4"><Data ss:Type="Boolean">1</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="Number">2</Data></Cell>
    <Cell><Data ss:Type="String">ss</Data></Cell>
    <Cell><Data ss:Type="String">s</Data></Cell>
    <Cell><Data ss:Type="Boolean">0</Data></Cell>
   </Row>
   <Row>
    <Cell><Data ss:Type="Number">3</Data></Cell>
    <Cell><Data ss:Type="String">ss</Data></Cell>
    <Cell><Data ss:Type="String">s</Data></Cell>
   </Row>
  </Table>
  <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
   <Selected/>
   <Panes>
    <Pane>
     <Number>3</Number>
     <ActiveRow>3</ActiveRow>
     <ActiveCol>6</ActiveCol>
    </Pane>
   </Panes>
   <ProtectObjects>False</ProtectObjects>
   <ProtectScenarios>False</ProtectScenarios>
  </WorksheetOptions> 
 </Worksheet>
</Workbook>


Actual XML File Result:

<?xml version="1.0" encoding="Windows-1252"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="urn:schemas-microsoft-com:office:spreadsheet">
  <DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
    <Author>Somebody</Author>
  </DocumentProperties>
  <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
    <WindowHeight>10230</WindowHeight>
    <WindowWidth>13275</WindowWidth>
    <WindowTopX>480</WindowTopX>
    <WindowTopY>75</WindowTopY>
    <ProtectStructure>False</ProtectStructure>
    <ProtectWindows>False</ProtectWindows>
  </ExcelWorkbook>
  <Styles>
    <Style d3p1:ID="Default" d3p2:Name="Normal" xmlns:d3p2="ss" xmlns:d3p1="urn:schemas-microsoft-com:office:spreadsheet">
      <d3p1:Alignment d3p2:Vertical="Bottom" />
      <d3p1:Font />
      <d3p1:Interior />
      <d3p1:NumberFormat />
      <d3p1:Protection />
    </Style>
  </Styles>
  <WorkSheet d2p1:Name="Sheet1" xmlns:d2p1="urn:schemas-microsoft-com:office:spreadsheet">
    <Table ExpandedColumnCount="1" ExpandedRowcount="1" TotalColumns="3" TotalRows="1">
      <d2p1:Row>
        <d2p1:Cell><Data ss:Type="String">ID</Data></d2p1:Cell>
        <d2p1:Cell><Data ss:Type="String">Name</Data></d2p1:Cell>
      </d2p1:Row>
      <d2p1:Row>
        <d2p1:Cell><Data ss:Type="Number">1</Data></d2p1:Cell>
        <d2p1:Cell><Data ss:Type="String">Sample</Data></d2p1:Cell>
      </d2p1:Row>
    </Table>
    <WorksheetOptions xmlns="urn:schemas-microsoft-com:office:excel">
      <x:Selected />
      <x:Panes>
        <x:Pane>
          <Number>3</Number>
          <ActiveRow>3</ActiveRow>
          <ActiveCol>3</ActiveCol>
        </x:Pane>
      </x:Panes>
      <ProtectObjects>False</ProtectObjects>
      <ProtectScenarios>False</ProtectScenarios>
    </WorksheetOptions>
  </WorkSheet>
</Workbook>


Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>