Saturday, September 17, 2011

Export Dataset to Excel in ASP.NET

 

Export grid to Excel. You can export the grid to excel sheet and give option to Save or Download
 the Excel Sheet.
In CS Page use the below code:
        DataClass objDataClass = new DataClass();
DataSet ds = objDataClass.GetDate();
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=SearchResults.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";

System.IO.StringWriter sw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

//Grid ID
gvDisplay.RenderControl(hw);

Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
In Aspx page use EnableEventValidation="false" in page directive

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.