Export Data from Gridview to Excel File in Asp.net C# with Example


Hello Friends, here in this post we will learn How to Export data from  gridview to Excel file. It is good to provide data in excel sheet. when your web application's data driven means allows user to export data from application for a record than Excel is best option because it is very popular and all users have knowledge of excel so He/She can manage data in their way.


In the following example we will use Render Control method providing content of server control. This method will confirms that an HTML Form control is rendered for the specified Asp.Net server control at run time.

 Screen shot of following application:-





When you click on "Export To Excel" Button it will ask for Download option "Open With" or "Save As". Choose Open with option it will open Excel sheet with gridview data like bellow.....




Source Code in (ASP.net):-

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridToExcel.aspx.cs" Inherits="BlogCodeTester.GridToXml" %>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:GridView ID="gvData" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:TemplateField HeaderText="Name">
                        <ItemTemplate>
                            <asp:Label ID="lblnm" runat="server" Text='<%#Eval("fname")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="Category">
                        <ItemTemplate>
                            <asp:Label ID="lblnm" runat="server" Text='<%#Eval("category")%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <FooterStyle BackColor="White" />
                <HeaderStyle BackColor="#43C0EE" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#C5DFEE" />
            </asp:GridView>
            <asp:Button ID="btn_exporttoecel" runat="server" OnClick="btn_exporttoecel_Click" Text="Export To  Excel" />
        </div>
    </form>
</body>
</html>

Code in C#:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace BlogCodeTester
{
    public partial class GridToXml : System.Web.UI.Page
    {
        DataClasses1DataContext dc = new DataClasses1DataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            gvData.DataSource = from a in dc.aby_temps select a;
            gvData.DataBind();
        }

        protected void btn_exporttoecel_Click(object sender, EventArgs e)
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "DataFromGrid.xls"));

            // given file name is "DataFromGrid" you can give as you want

            Response.ContentType = "application/ms-excel";
            StringWriter stringWrite = new StringWriter();
            HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

            gvData.RenderControl(htmlWrite);
            Response.Write(stringWrite.ToString());
            Response.End();
        }
        public override void VerifyRenderingInServerForm(Control control)
        {
            // Confirms that an HtmlForm control is rendered for the
        }

    }
}

3 comments

ok one new task of u excel data direct upload in ms-sql make this example this is good task to upload in blog

Reply

Thanks For Comment...........

Here is the post that you want

How to Import Data from Excel Sheet

Reply

Post a Comment