Click here to Skip to main content
15,921,382 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I want to export the DataSet values into excel sheet.
Posted

Pls try it.....

public void ExportToExcel(DataTable dt)
{
if (dt.Rows.Count > 0)
{
string filename = "ExcelFileName.xls";
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
DataGrid dgGrid = new DataGrid();
dgGrid.DataSource = dt;
dgGrid.DataBind();

//Get the HTML for the control.
dgGrid.RenderControl(hw);
//Write the HTML back to the browser.
//Response.ContentType = application/vnd.ms-excel;
Response.ContentType = "application/vnd.ms-excel";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + "");
this.EnableViewState = false;
Response.Write(tw.ToString());
Response.End();
}
}

protected void btnSave_Click(object sender, EventArgs e)
{
ExportToExcel((DataTable)ViewState["gvMobile"]);
}

public override void VerifyRenderingInServerForm(Control control)

{

}
 
Share this answer
 
 
Share this answer
 
 
Share this answer
 
Comments
Maciej Los 27-Aug-12 6:25am    
+5!
Prasad_Kulkarni 27-Aug-12 6:37am    
Thank you Iosmac!
__TR__ 27-Aug-12 6:32am    
My 5!
Prasad_Kulkarni 27-Aug-12 6:38am    
Thank you TR!
Manas Bhardwaj 27-Aug-12 8:36am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900