Today I want to show you another way how to export Excel file from HtmlForm object. This is a serial post for How to export Excel file. Using HtmlForm object we should use a Control which is rendering by HtmlTextWriter.
In this article, I use the GridView control. Here I provide a method where you pass a GridView control as parameter after that this method render this gridview control as a excel file from html object.
Code:C#
Namespace:
using System.Web.UI.HtmlControls
public void ExportFromHtmlForm(GridView gvEmployee)
{
HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new HtmlTextWriter(stw);
form.Controls.Add(gvEmployee);
this.Controls.Add(form);
form.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
}
Sharing real-world experiences about C#, Dynamics CRM, Dynamics 365, Dynamics NAV/Business Central, SharePoint,PowerBI,ASP.net and more...
Subscribe to:
Post Comments (Atom)
Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'
Exception: Method 'StartWorkflowOnListItem' in type 'Microsoft.SharePoint.WorkflowServices.FabricWorkflowInstanceProvider'...
-
How to get the MAC address of system using Asp.net/C# Now I want to share with you some simple but important for our rapid development. ...
-
Today I have faced a problem when working with a previous developed ASP.NET MVC application. Just copy the database(.sdf) from previous ver...
-
Last day I have faced a problem when working with "Anonymous Type". In development procedure I have got result from a service w...
3 comments:
Thanks, used this code. Nice & tidy!
welcome...
This works fine with a simple table, I have a table where I have added an extra header row as part of the grid bind and the export dows not include the extra row and is dropping the last data row.
Post a Comment