Export gridview content from code behind in excel using .net
how Export grid
view content from code behind in excel using .net
Below is the Code for export to excel :
public static void ExportToExcel(GridView gridObject, string excelSheetName)
{
try
{
var style = @" ";
var attachment = "attachment; filename=" + excelSheetName + ".xls";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/ms-excel";
var sw = new StringWriter();
var htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
var frm = new HtmlForm();
gridObject.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gridObject);
frm.RenderControl(htw);
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write("
");
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
catch (Exception)
{
}
}
There is some time we need in a project's reporting module to export
grid view data to execel.by understanding that requirement i decided to write
this article especially focusing on beginners and those who want to learn
Export grid view content from code behind in excel using asp.net.
Now let us create a function to export zip data file in asp.net. Please refer to my following article.
For more details about handler you can take reference of MSDN.
Go for Client side Multiple parameter refer my post:
Client
side ajax post using multiple parameterBelow is the Code for export to excel :
public static void ExportToExcel(GridView gridObject, string excelSheetName)
{
try
{
var style = @" ";
var attachment = "attachment; filename=" + excelSheetName + ".xls";
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.AddHeader("content-disposition", attachment);
HttpContext.Current.Response.ContentType = "application/ms-excel";
var sw = new StringWriter();
var htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
var frm = new HtmlForm();
gridObject.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
frm.Controls.Add(gridObject);
frm.RenderControl(htw);
HttpContext.Current.Response.Write(style);
HttpContext.Current.Response.Write("
" + excelSheetName + " |
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
catch (Exception)
{
}
}
Export gridview content from code behind in excel using .net
Reviewed by Vikas Kumar Singh
on
December 11, 2017
Rating:
No comments: