Web developers wiki ASP.NET Sitecore Sharepoint Kentico by Evident Interactive

How to export xlsheet from a webpage

Modified: 2009/02/11 14:16 by jim - Categorized as: ASP.NET, Csharp
The code below will cause a open/save file dialog to popup, enabling you to save an xls document. The xls generator can even be substituted by any control, but a good example would be a grid or gridview.

System.Web.UI.UserControl uc = new System.Web.UI.UserControl();

using (StringWriter sw = new StringWriter())
{
        // SomeXlsGeneratingObject.ascx has some code-before that defines an xlsheet
	SomeXlsGeneratingObject xlsGenerator = (SomeXlsGeneratingObject)uc.LoadControl("~/UserControls/SomeXlsGeneratingObject.ascx");

	xlsGenerator.generate();
	//Create HtmlTextWriter object
	HtmlTextWriter htw = new HtmlTextWriter(sw);
	//Rendercontrol en set output to stream
	xlsGenerator.RenderControl(htw);
	Response.ContentType = "application/vnd.ms-excel";
	Response.Write(sw.ToString());
	Response.End();

}

 © Evident Interactive BV