title: Export xml data to Excel using OpenXML
body: <p>The file is downloading an xlsx file but when i tried to open the file it is saying <strong>file is corrupted</strong>. Here is the code i'm trying to use please let me know if any changes has to be done for the following.</p>
  
  <pre><code>private void button1_Click(object sender, EventArgs e)
  {
      ArrayList DataNode = new ArrayList();
      XmlDocument xmlobj = new XmlDocument();
      ArrayList FinalXML = new ArrayList();
      XslCompiledTransform xXslt = new XslCompiledTransform();
      xmlobj.Load(@"D:\ExcelImport\Input.xml");
      xXslt.Load(@"D:\ExcelImport\demoxsl.xslt");
      XmlNodeList DN ;
      DN = xmlobj.DocumentElement.GetElementsByTagName("Data");
      for (int i = 0; i &lt; DN.Count; i++)
      {
          DataNode.Add("&lt;ShaleDataExport&gt;&lt;Data Flag = '" + i + "' &gt;" + DN.Item(i).InnerXml + "&lt;/Data&gt;&lt;/ShaleDataExport&gt;");    
      }
      string ShaleDataExportXML;
      int k = 0 ;
      while (k &lt; DN.Count)
      {
          ShaleDataExportXML = DataNode[k].ToString();
          XmlDocument xml =  new XmlDocument();
          xml.LoadXml(ShaleDataExportXML);
          StringWriter sw = new StringWriter();  
          xXslt.Transform(xml, null, sw);
          FinalXML.Add(sw);
          sw.Close();
          k++;
      }
      using (SpreadsheetDocument doc = SpreadsheetDocument.Create(@"D:\ExcelImport\OutPut\OutPut.xlsx", DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
      {
          WorkbookPart workbook = doc.AddWorkbookPart();
          string XML;
          string WorbookXML;
          WorbookXML = @"&lt;?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?&gt;&lt;workbook xmlns=""schemas.openxmlformats.org/.../main"" xmlns:r=""schemas.openxmlformats.org/.../relationships""&gt;&lt;sheets&gt;";
          for (int j = 0; j &lt; DN.Count; j++)
          {
              WorksheetPart[] sheet = new WorksheetPart[DN.Count];
              sheet[j] = workbook.AddNewPart&lt;WorksheetPart&gt;();
              string sheetId = workbook.GetIdOfPart(sheet[j]);
              XML = @"&lt;?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes""?&gt;&lt;worksheet xmlns=""schemas.openxmlformats.org/.../main"" &gt;";
              XML += FinalXML[j].ToString() + "&lt;/worksheet&gt;";
              string SheetXML = XML.ToString();
              XmlDocument SXML = new XmlDocument();
              SXML.LoadXml(SheetXML);
              byte[] byteArray = Encoding.ASCII.GetBytes(SXML.OuterXml);
              MemoryStream stream = new MemoryStream(byteArray);
              StreamReader reader = new StreamReader(stream);
              string text = reader.ReadToEnd();
              WorbookXML += "&lt;sheet name="+ AddPartXml(sheet[j], text) + " sheetId=" + j.ToString() + "  r:id=" + sheetId.ToString() + " /&gt;";
          }
          WorbookXML += "&lt;/sheets&gt;&lt;/workbook&gt;";
          AddPartXml(workbook, WorbookXML);
          doc.Close();
      }
  }
  public string  AddPartXml(OpenXmlPart part, string xml)
  {
      Uri uri = part.Uri;
      String[] sheetNames = uri.OriginalString.Split('/');
      string sheetName = sheetNames[sheetNames.Length - 1].Split('.')[0];
      using (Stream stream = part.GetStream())
      {
          byte[] buffer = (new UTF8Encoding()).GetBytes(xml);
          stream.Write(buffer, 0, buffer.Length);
      }
      return sheetName;
  }
  </code></pre>
  
  <p>Thanks in advance </p>
  
  <p>Vineet Mangal</p>