Initial chamber
Step 1: Open Visual Studio 2010 and create an empty website Give a suitable name: gridviewexcel_demo.
Step 2: In Solution Explorer you will get your empty website, then add a Web Form and SQL Server Database. By going like the following:
For Web Form gridviewexcel_demo (Your Empty Website) - Right Click, Add New Item, then Web Form. Name it gridviewexcel_demo.aspx.
For SQL Server Database: gridviewexcel_demo (Your Empty Website) - Right Click, Add New Item, then SQL Server Database. Add Database inside the App_Data_folder.
Database chamber Step 3: In Server Explorer, click on your Database
Database.mdf - Tables, Add New Table and make a table like the following:
Table - tbl_data (Don’t Forget to make ID as IS Identity - True)
Design chamber Step 4: Now make some design for your application by going to gridviewexcel_demo.aspx and try the following code snippet:
- <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" EnableEventValidation = "false" 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></title>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
-
- <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
- CellPadding="4" ForeColor="#333333" GridLines="None">
- <AlternatingRowStyle BackColor="White" />
- <Columns>
-
- <asp:TemplateField>
-
- <ItemTemplate>
-
-
- <asp:CheckBox ID="Checkbox1" runat="server" />
- </ItemTemplate>
-
-
-
-
- </asp:TemplateField>
-
-
- <asp:TemplateField HeaderText="ID">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("id") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="Name">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
- </ItemTemplate>
- </asp:TemplateField>
- <asp:TemplateField HeaderText="City">
- <EditItemTemplate>
- <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
- </EditItemTemplate>
- <ItemTemplate>
- <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label>
- </ItemTemplate>
-
-
- </asp:TemplateField>
- </Columns>
- <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
- <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
- <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
- <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
- <SortedAscendingCellStyle BackColor="#FDF5AC" />
- <SortedAscendingHeaderStyle BackColor="#4D0000" />
- <SortedDescendingCellStyle BackColor="#FCF6C0" />
- <SortedDescendingHeaderStyle BackColor="#820000" />
- </asp:GridView>
-
- <br />
-
- </div>
- <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
- Text="Export to Excel" />
- </form>
- </body>
- </html>
You’re design looks like the following code snippet:
Code chamber Before this coding part you need to download iTextsharp library from the souceforge website, here is the link just download the file, the file is in zip format, so just unzipped the files at suitable location, so that we can easily browse.
Itextsharp Library download - -
itextsharp-all-5.5.6 After you download the file, please check that you download the right file or not. Here's an image:
Step 5: Now it’s time for server side coding so that our application gets working. Open your gridviewexcel_demo.aspx.cs file and code it like the following.
Firstly, import the dll of ItextSharp by going to your empty website
gridviewexcel_demo. Right click and Add References.
Now browse the ItextSharp unzip file and open it. Make sure you got all unzip dll files into your application. See the following image to know what .dll you have to import.
Now import these namespaces, since it will be needed when we write the code for exporting the GridView to excel.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.IO;
- using System.Text;
- using iTextSharp.text;
- using iTextSharp.text.html;
- using iTextSharp.text.html.simpleparser;
- using iTextSharp.text.pdf;
-
-
- public partial class _Default : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- if (!Page.IsPostBack)
- {
- refreshdata();
- }
- }
-
- public void refreshdata()
- {
-
- SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
- SqlDataAdapter sda = new SqlDataAdapter(cmd);
- DataTable dt = new DataTable();
- sda.Fill(dt);
- GridView1.DataSource = dt;
- GridView1.DataBind();
-
-
-
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- bool isselected = false;
-
- foreach (GridViewRow gvrow in GridView1.Rows)
- {
- CheckBox chck = gvrow.FindControl("Checkbox1") as CheckBox;
- if (chck != null && chck.Checked)
- {
- isselected = true;
- break;
- }
- }
- if (isselected)
- {
- GridView grdxport = GridView1;
-
- grdxport.Columns[0].Visible = false;
- foreach (GridViewRow gvrow in GridView1.Rows)
- {
- grdxport.Rows[gvrow.RowIndex].Visible = false;
- CheckBox chck = gvrow.FindControl("Checkbox1") as CheckBox;
- if (chck != null && chck.Checked)
- {
- grdxport.Rows[gvrow.RowIndex].Visible = true;
- }
- }
-
- Response.Clear();
- Response.Buffer = true;
- Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
- Response.Charset = "";
-
- Response.ContentType = "application/vnd.ms-excel";
-
-
- StringWriter swr = new StringWriter();
- HtmlTextWriter htmlwtr = new HtmlTextWriter(swr);
- grdxport.RenderControl(htmlwtr);
- Response.Output.Write(swr.ToString());
- Response.End();
-
- }
-
- }
-
- public override void VerifyRenderingInServerForm(Control control)
- {
-
- }
- }
Output chamber In Excel Sheet: