skip to main
|
skip to sidebar
Asp.net Examples
Monday, July 28, 2008
Export to Excel Using Styles
public void ExportDataGridToExcel(System.Web.UI.Control ctrl, System.Web.HttpResponse response)
{
response.Clear();
response.Buffer= true;
response.Cache.SetCacheability(HttpCacheability.NoCache);
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("content-disposition", "attachment;filename=Projects.xls");
response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.ClearControls(ctrl);
ctrl.RenderControl(oHtmlTextWriter);
// set content type and character set to cope with european chars like the umlaut.
response.Write("
\n");
// add the style props to get the page orientation
response.Write(AddExcelStyling());
response.Write(oStringWriter.ToString());
response.Write("