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. <head id="Head1">
  3. <title>Export Gridview with Images in Asp.net</title>
  4. <style type="text/css">
  5. .GridviewDesign {
  6. font-size: 100%;
  7. font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif color: #303933;
  8. }
  9. .headerstyleForGrid {
  10. color: #FFFFFF;
  11. border-right-color: #abb079;
  12. border-bottom-color: #abb079;
  13. background-color: #df5015;
  14. padding: 0.5em 0.5em 0.5em 0.5em;
  15. text-align: center;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <form id="form1" runat="server">
  21. <div class="GridviewDesign">
  22. <asp:GridView ID="gvDetailsForExport" CssClass="Gridview" runat="server" AutoGenerateColumns="False">
  23. <HeaderStyle CssClass="headerstyleForGrid" />
  24. <Columns>
  25. <asp:BoundField HeaderText="User Id" DataField="UserId" />
  26. <asp:BoundField HeaderText="User Name" DataField="UserName" />
  27. <asp:BoundField HeaderText="Gender" DataField="Gender" />
  28. <asp:ImageField DataImageUrlField="Imagepath" HeaderText="Image" ItemStyle-Height="25px" ItemStyle-Width="25px" /> </Columns>
  29. </asp:GridView> <br />
  30. <asp:Button ID="btnExportToExcel" runat="server" Text="Export Data To Excel" onclick="btnExport_Click" />
  31. <asp:Button ID="btnExportToWord" runat="server" Text="Export Data To Word" onclick="btnExportToWord_Click" /> </div>
  32. </form>
  33. </body>
  34. </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.