Export data to XLS/XML file - Online Demo
Source
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
Response.Clear();
Response.ClearHeaders();
Response.CacheControl = "public";
Response.Cache.SetCacheability(HttpCacheability.Public);
Response.AddHeader("Content-Disposition", "attachment; filename=sample.xls");
Response.ContentType = "application/vnd.ms-excel";
DateTime d1 = DateTime.Now;
DateTime d2 = DateTime.Now.AddDays(1);
Stream stream = Response.OutputStream;
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml
(
"<sample>" +
String.Format("<row number='{0}' date='{1}'></row>", d1.Day.ToString(), d1.ToString("s")) +
String.Format("<row number='{0}' date='{1}'></row>", d2.Day.ToString(), d2.ToString("s")) +
"</sample>"
);
XslCompiledTransform xlsTransform = new XslCompiledTransform();
xlsTransform.Load(Server.MapPath("~/eg_samples/ExportXLS_demo.xsl"));
xlsTransform.Transform((IXPathNavigable)xmlDocument, null, stream);
Response.End();