-->

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
  1. <div style="padding:10px">
  2. <h3>Export Gridview Selected Rows in ASP.NET</h3>
  3. <br />
  4. <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellSpacing="10">
  5. <Columns>
  6. <asp:TemplateField>
  7. <ItemTemplate>
  8. <asp:CheckBox ID="chkSelect" runat="server" />
  9. </ItemTemplate>
  10. </asp:TemplateField>
  11. <asp:BoundField DataField="Country" HeaderText="Country" />
  12. <asp:BoundField DataField="State" HeaderText="State" />
  13. <asp:BoundField DataField="City" HeaderText="City" />
  14. </Columns>
  15. </asp:GridView>
  16. <br />
  17. <asp:Button ID="btnExport" runat="server" Text="Export Selected Rows" OnClick="btnExport_Click" />
  18. </div>

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


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

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


  1. protected void btnExport_Click(object sender, EventArgs e)
  2. {
  3. // Export Selected Rows to Excel file Here
  4.  
  5. // need to check is any row selected
  6. bool isSelected = false;
  7. foreach (GridViewRow i in GridView1.Rows)
  8. {
  9. CheckBox cb = (CheckBox)i.FindControl("chkSelect");
  10. if (cb != null && cb.Checked)
  11. {
  12. isSelected = true;
  13. break;
  14. }
  15. }
  16.  
  17. // export here
  18. if (isSelected)
  19. {
  20. GridView gvExport = GridView1;
  21. // this below line for not export checkbox to excel file
  22. gvExport.Columns[0].Visible = false;
  23. foreach (GridViewRow i in GridView1.Rows)
  24. {
  25. gvExport.Rows[i.RowIndex].Visible = false;
  26. CheckBox cb = (CheckBox)i.FindControl("chkSelect");
  27. if (cb != null && cb.Checked)
  28. {
  29. gvExport.Rows[i.RowIndex].Visible = true;
  30. }
  31. }
  32.  
  33. Response.Clear();
  34. Response.Buffer = true;
  35. Response.AddHeader("content-disposition", "attachment;filename=ExportGridData.xls");
  36. Response.Charset = "";
  37. Response.ContentType = "application/vnd.ms-excel";
  38. StringWriter sw = new StringWriter();
  39. HtmlTextWriter htW = new HtmlTextWriter(sw);
  40. gvExport.RenderControl(htW);
  41. Response.Output.Write(sw.ToString());
  42. Response.End();
  43. }
  44. }

Step-8: Run Application.

Download  Live Demo


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.

Related Posts: