How to Save GridView Data in Excel using ASP.NET

  1. <asp:GridView ID="GridView1" runat="server" BackColor="#CCCCCC" BorderColor="#999999"  
  2. BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black"  
  3. Height="208px" Width="324px">  
  4.     <FooterStyle BackColor="#CCCCCC" />  
  5.     <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />  
  6.     <PagerStyle BackColor="#CCCCCC" ForeColor="Black" Horizontal />  
  7.     <RowStyle BackColor="White" />  
  8.     <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />  
  9.     <SortedAscendingCellStyle BackColor="#F1F1F1" />  
  10.     <SortedAscendingHeaderStyle BackColor="#808080" />  
  11.     <SortedDescendingCellStyle BackColor="#CAC9C9" />  
  12.     <SortedDescendingHeaderStyle BackColor="#383838" />  
  13. </asp:GridView>  
  14.   
  15.    Select Name  
  16. <br />  
  17. <asp:DropDownList ID="DropDownList1" runat="server" Height="24px" Width="105px" ForeColor="#0099FF">  
  18.     <asp:ListItem>Select a Name</asp:ListItem>  
  19. </asp:DropDownList>  
  20. <asp:Button ID="Button1" runat="server" Text="Submit" />  
  1. Imports System.Drawing  
  2. Imports System.Data.SqlClient  
  3. Imports System.Data  
  4. Partial Class Templet_css_GridView  
  5. Inherits System.Web.UI.Page  
  6. Dim conn As New SqlConnection("Path")  
  7. Dim cmd As New SqlCommand()  
  8. Protected Sub Button1_Click(ByVal sender As ObjectByVal e As System.EventArgs) Handles Button1.Click  
  9. Response.ClearContent()  
  10. Response.AddHeader("content-disposition""attachment; filename=gvtoexcel.xls")  
  11. Response.ContentType = "application/excel"  
  12. Dim sw As New System.IO.StringWriter()  
  13. Dim htw As New HtmlTextWriter(sw)  
  14. GridView1.RenderControl(htw)  
  15. Response.Write(sw.ToString())  
  16. Response.[End]()  
  17. DropDownList1.SelectedItem.ToString()  
  18.   
  19. End Sub  
  20. Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)  
  21. 'Tell the compiler that the control is rendered  
  22. 'explicitly by overriding the VerifyRenderingInServerForm event.  
  23. End Sub  
  24.   
  25. Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  26. Dim adapter As New SqlDataAdapter()  
  27. Dim ds As New DataSet()  
  28. Dim i As Integer = 0  
  29. Dim sql As String = Nothing  
  30. Dim connetionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=D:\manish\WebSite\App_Data\mani.mdf;Integrated Security=True;User Instance=True"  
  31. sql = "select name,roll,monumber,coll,class from my"  
  32. Dim connection As New SqlConnection(connetionString)  
  33. connection.Open()  
  34. Dim command As New SqlCommand(sql, connection)  
  35. adapter.SelectCommand = command  
  36. adapter.Fill(ds)  
  37. adapter.Dispose()  
  38. command.Dispose()  
  39. connection.Close()  
  40. GridView1.DataSource = ds.Tables(0)  
  41. GridView1.DataBind()  
  42. DropDownList1.DataSource = ds.Tables(0)  
  43. DropDownList1.DataTextField = "name"  
  44. DropDownList1.DataBind()  
  45.   
  46. 'TextBox1.Text = "fffffffff"  
  47. End Sub