Export gridview to excel within an UpdatePanel in asp.net using c#

In this article we will learn how to convert gridview to Excel using C#.
Just Drag and drop one gridview control.

In Code Behind page:-

[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}

}
private void BindData()
{
string query = "select * from a1";
SqlConnection myConnection = new SqlConnection("server=servername;uid=userid;password=password;database=databasename");
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "TableName");
grvGridToExcel.DataSource = ds;
grvGridToExcel.DataBind();
}


public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */

}
protected void BtnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
grvGridToExcel.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

}

[/code]

In ASPX Page:-

[code]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div> <asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>


<asp:GridView ID="grvGridToExcel" runat="server">
</asp:GridView>
<asp:Button ID="BtnExport" OnClick="BtnExport_Click" runat="server" Text="ExportGridtoExcel" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="BtnExport" />
</Triggers>
</asp:UpdatePanel>
</div>
</form>
</body>
</html>


[/code]

1 comment:

  1. Dear Blogger,

    This is Lacy from e-iceblue.I am very happy to read this article and want to tell you that our product Spire.DataExport and its free edition Spire.Free DataExport, Spire.Xls for .NET all can export data from datafridview to Excel. So I would like to invite you to add a solution after this article. You can choose any one of the three or both to reaize this task.The more solutions the better this article, the more readers. So tell me if you accept me.
    Below link can help you:
    Spire.DataExport and Spire.Free DataExport:
    http://www.e-iceblue.com/Knowledgebase/Spire.DataExport/Program-Guide/How-to-Export-Datatable-to-Excel-through-DataGridView.html
    Spire.XLS:
    http://www.e-iceblue.com/Knowledgebase/Spire.XLS/Program-Guide/Data-Export-/Import-Export-Datatable-to-Excel-from-Database.html

    Sorry to disturb.Have a nice day!

    ReplyDelete

Plz Share your comments...