CodeKicks.com
Focus on Microsoft Technologies - Tutorials, Articles, Code Samples.

Wednesday, April 04, 2007

Exporting data from datagrid to excel


DataGrid Dg = new DataGrid();


Dg.HeaderStyle.Font.Bold=true;


Dg.HeaderStyle.ForeColor=Color.Blue;


Dg.DataSource = DSCustRepFormsListExcel;


Dg.DataBind();


Response.Clear();


Response.Buffer= true;


Response.ContentType = "application/vnd.ms-excel";


string fileName = "CustomReportsForms.xls";


Response.AddHeader("Content-Disposition","FileName=" +fileName);


Response.ContentEncoding = System.Text.Encoding.UTF7;


Response.Charset = "";


EnableViewState = false;


System.IO.StringWriter oStringWriter = new System.IO.StringWriter();


System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);


Dg.RenderControl(oHtmlTextWriter);


Response.Write(oStringWriter.ToString());


Dg.Dispose();


Response.End();

Post a Comment