Export Grid Data into Excel and Word

Here we will learn how to export grid data into Excel and Word. Add an aspx page and write the following code.
  1. <html xmlns="http://www.w3.org/1999/xhtml">  
  2.   
  3. <head id="Head1">  
  4.     <title>Export Gridview with Images in Asp.net</title>  
  5.     <style type="text/css">  
  6.         .GridviewDesign {  
  7.             font-size: 100%;  
  8.             font-family: 'Trebuchet MS''Lucida Sans Unicode''Lucida Grande''Lucida Sans', Arial, sans-serif color: #303933;  
  9.         }  
  10.           
  11.         .headerstyleForGrid {  
  12.             color: #FFFFFF;  
  13.             border-right-color: #abb079;  
  14.             border-bottom-color: #abb079;  
  15.             background-color: #df5015;  
  16.             padding: 0.5em 0.5em 0.5em 0.5em;  
  17.             text-align: center;  
  18.         }  
  19.     </style>  
  20. </head>  
  21.   
  22. <body>  
  23.     <form id="form1" runat="server">  
  24.         <div class="GridviewDesign">  
  25.             <asp:GridView ID="gvDetailsForExport" CssClass="Gridview" runat="server" AutoGenerateColumns="False">  
  26.                 <HeaderStyle CssClass="headerstyleForGrid" />  
  27.                 <Columns>  
  28.                     <asp:BoundField HeaderText="User Id" DataField="UserId" />  
  29.                     <asp:BoundField HeaderText="User Name" DataField="UserName" />  
  30.                     <asp:BoundField HeaderText="Gender" DataField="Gender" />  
  31.                     <asp:ImageField DataImageUrlField="Imagepath" HeaderText="Image" ItemStyle-Height="25px" ItemStyle-Width="25px" /> </Columns>  
  32.             </asp:GridView> <br />  
  33.             <asp:Button ID="btnExportToExcel" runat="server" Text="Export Data To Excel" onclick="btnExport_Click" />    
  34.             <asp:Button ID="btnExportToWord" runat="server" Text="Export Data To Word" onclick="btnExportToWord_Click" /> </div>  
  35.     </form>  
  36. </body>  
  37.   
  38. </html>  
After coding for .aspx page write the following code on .aspx.cs page,
  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     if (!IsPostBack)  
  4.     {  
  5.         gvDetailsForExport.DataSource = BindData();  
  6.         gvDetailsForExport.DataBind();  
  7.     }  
  8. }  
  9. protected DataTable BindData()  
  10. {  
  11.     DataTable dtGrid = new DataTable();  
  12.     dtGrid.Columns.Add("UserId"typeof(Int32));  
  13.     dtGrid.Columns.Add("UserName"typeof(string));  
  14.     dtGrid.Columns.Add("Gender"typeof(string));  
  15.     dtGrid.Columns.Add("Imagepath"typeof(string));  
  16.     dtGrid.Rows.Add(1, "Munesh""Male""Images/uploads/Sign1.jpg");  
  17.     dtGrid.Rows.Add(2, "Rahul""Male""Images/uploads/Sign2.jpg");  
  18.     dtGrid.Rows.Add(3, "Reet""Female""Images/uploads/Sign3.jpg");  
  19.     dtGrid.Rows.Add(4, "Anshuman""Male""Images/uploads/Sign4.jpg");  
  20.     return dtGrid;  
  21. }  
  22. protected void btnExport_Click(object sender, EventArgs e)  
  23. {  
  24.     Response.ClearContent();  
  25.     Response.Buffer = true;  
  26.     Response.AddHeader("content-disposition"string.Format("attachment; filename={0}""Employees.xls"));  
  27.     Response.ContentType = "application/ms-excel";  
  28.     StringWriter sw = new StringWriter();  
  29.     HtmlTextWriter htw = new HtmlTextWriter(sw);  
  30.     gvDetailsForExport.AllowPaging = false;  
  31.     gvDetailsForExport.DataSource = BindData();  
  32.     gvDetailsForExport.DataBind();  
  33.     gvDetailsForExport.HeaderRow.Style.Add("background-color""#FFFFFF");  
  34.     for (int i = 0; i < gvDetailsForExport.HeaderRow.Cells.Count; i++)  
  35.     {  
  36.         gvDetailsForExport.HeaderRow.Cells[i].Style.Add("background-color""#df5015");  
  37.     }  
  38.     gvDetailsForExport.RenderControl(htw);  
  39.     Response.Write(sw.ToString());  
  40.     Response.End();  
  41. }  
  42. protected void btnExportToWord_Click(object sender, EventArgs e)  
  43. {  
  44.     Response.ClearContent();  
  45.     Response.Buffer = true;  
  46.     Response.AddHeader("content-disposition"string.Format("attachment; filename={0}""Employees.doc"));  
  47.     Response.ContentType = "application/ms-word";  
  48.     StringWriter sw = new StringWriter();  
  49.     HtmlTextWriter htw = new HtmlTextWriter(sw);  
  50.     gvDetailsForExport.AllowPaging = false;  
  51.     gvDetailsForExport.DataSource = BindData();  
  52.     gvDetailsForExport.DataBind();  
  53.     //Change the Header Row back to white color  
  54.     gvDetailsForExport.HeaderRow.Style.Add("background-color""#FFFFFF");  
  55.     //Applying stlye to gridview header cells  
  56.     for (int i = 0; i < gvDetailsForExport.HeaderRow.Cells.Count; i++)  
  57.     {  
  58.         gvDetailsForExport.HeaderRow.Cells[i].Style.Add("background-color""#df5015");  
  59.     }  
  60.     gvDetailsForExport.RenderControl(htw);  
  61.     Response.Write(sw.ToString());  
  62.     Response.End();  
  63. }  
Now run your application and see the output and perform the operation on Gridview.
Ebook Download
View all
Learn
View all