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'...
-
In asp.net gridview control is widely used control. It has lots of properties and attributes which help the developers for rapid development...
-
Today I have added Google Map my asp.net application successfully !! Don't worry it's not a big deal. Very easy and you will find a ...
-
Today I want to discuss with you how to implement Observer design pattern using delegate and event in asp.net /c#. Observer Pattern The...
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