How to export selected rows from gridview to excel in asp.net

Introduction

In this post I am explain how to export selected rows from gridview to excel in asp.net

Here i am writing this article to explain how to export selected rows from gridview to excel in asp.net.

Steps :

Step - 1 : Create New Project.

Go to File > New > Project > Select asp.net web forms application > Entry Application Name > Click OK.

Step-2: Add a Database.

Go to Solution Explorer > Right Click on App_Data folder > Add > New item > Select SQL Server Database Under Data > Enter Database name > Add.

Step-3: Create table for fetch data.

Open Database > Right Click on Table > Add New Table > Add Columns > Save > Enter table name > Ok.
In this example, I have used one tables as below

Step-4: Add Entity Data Model.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select ADO.net Entity Data Model under data > Enter model name > Add.
A popup window will come (Entity Data Model Wizard) > Select Generate from database > Next >
Chose your data connection > select your database > next > Select tables > enter Model Namespace > Finish.

Step-5: Add a Webpage and Design for Show Data in Gridview & Export Selected rows to Excel File.

Go to Solution Explorer > Right Click on Project name form Solution Explorer > Add > New item > Select web form/ web form using master page under Web > Enter page name > Add.

HTML Code
              <div style="padding:10px">
                <h3>Export Gridview Selected Rows in ASP.NET</h3>
                <br />
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellSpacing="10">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                <asp:CheckBox ID="chkSelect" runat="server" />
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:BoundField DataField="Country" HeaderText="Country" />
                        <asp:BoundField DataField="State" HeaderText="State" />
                        <asp:BoundField DataField="City" HeaderText="City" />
                    </Columns>
                </asp:GridView>
                <br />
                <asp:Button ID="btnExport" runat="server" Text="Export Selected Rows" OnClick="btnExport_Click" />
            </div>
        

Step-6: Write following code in Page_Load event for Show data in Gridview.


             protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack)
                {
                    PopulateData();
                }
            }
        
Here is the function...
            private void PopulateData()
            {
                using (MyDatabaseEntities dc = new MyDatabaseEntities())
                {
                    GridView1.DataSource = dc.CityMasters.OrderBy(a=>a.Country).ThenBy(a=>a.State).ThenBy(a=>a.City).ToList();
                    GridView1.DataBind();
                }
            }
        

Step-7: Write below code in button click event for export Selected row from gridview to Excel File.


             protected void btnExport_Click(object sender, EventArgs e)
            {
                // Export Selected Rows to Excel file Here

                // need to check is any row selected 
                bool isSelected = false;
                foreach (GridViewRow i in GridView1.Rows)
                {
                    CheckBox cb = (CheckBox)i.FindControl("chkSelect");
                    if (cb != null && cb.Checked)
                    {
                        isSelected = true;
                        break;
                    }
                }

                // export here
                if (isSelected)
                {
                    GridView gvExport = GridView1;
                    // this below line for not export checkbox to excel file
                    gvExport.Columns[0].Visible = false;
                    foreach (GridViewRow i in GridView1.Rows)
                    {
                        gvExport.Rows[i.RowIndex].Visible = false;
                        CheckBox cb = (CheckBox)i.FindControl("chkSelect");
                        if (cb != null && cb.Checked)
                        {
                            gvExport.Rows[i.RowIndex].Visible = true;
                        }
                    }

                    Response.Clear();
                    Response.Buffer = true;
                    Response.AddHeader("content-disposition", "attachment;filename=ExportGridData.xls");
                    Response.Charset = "";
                    Response.ContentType = "application/vnd.ms-excel";
                    StringWriter sw = new StringWriter();
                    HtmlTextWriter htW = new HtmlTextWriter(sw);
                    gvExport.RenderControl(htW);
                    Response.Output.Write(sw.ToString());
                    Response.End();
                }
            }
        

Step-8: Run Application.



Related Post:
  1. How to export gridview to excel & Word file in asp.net
  2. How to export gridview to excel & Word file with formatting in asp.net
  3. Steps for import / export database data from/to CSV file.
  4. Steps for import / export database data from/to XML file.
  5. How to export database data to PDF file in ASP.NET.
  6. How to import / export database data from/to XML file.

Hello ! My name is Sourav Mondal. I am a software developer working in Microsoft .NET technologies since 2010.

I like to share my working experience, research and knowledge through my site.

I love developing applications in Microsoft Technologies including Asp.Net webforms, mvc, winforms, c#.net, sql server, entity framework, Ajax, Jquery, web api, web service and more.