TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
How to Save GridView Data in Excel using ASP.NET
Manish Pipaliya
May 05
2015
Code
1.5
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
<
asp:GridView
ID
=
"GridView1"
runat
=
"server"
BackColor
=
"#CCCCCC"
BorderColor
=
"#999999"
BorderStyle
=
"Solid"
BorderWidth
=
"3px"
CellPadding
=
"4"
CellSpacing
=
"2"
ForeColor
=
"Black"
Height
=
"208px"
Width
=
"324px"
>
<
FooterStyle
BackColor
=
"#CCCCCC"
/>
<
HeaderStyle
BackColor
=
"Black"
Font-Bold
=
"True"
ForeColor
=
"White"
/>
<
PagerStyle
BackColor
=
"#CCCCCC"
ForeColor
=
"Black"
Horizontal
/>
<
RowStyle
BackColor
=
"White"
/>
<
SelectedRowStyle
BackColor
=
"#000099"
Font-Bold
=
"True"
ForeColor
=
"White"
/>
<
SortedAscendingCellStyle
BackColor
=
"#F1F1F1"
/>
<
SortedAscendingHeaderStyle
BackColor
=
"#808080"
/>
<
SortedDescendingCellStyle
BackColor
=
"#CAC9C9"
/>
<
SortedDescendingHeaderStyle
BackColor
=
"#383838"
/>
</
asp:GridView
>
Select Name
<
br
/>
<
asp:DropDownList
ID
=
"DropDownList1"
runat
=
"server"
Height
=
"24px"
Width
=
"105px"
ForeColor
=
"#0099FF"
>
<
asp:ListItem
>
Select a Name
</
asp:ListItem
>
</
asp:DropDownList
>
<
asp:Button
ID
=
"Button1"
runat
=
"server"
Text
=
"Submit"
/>
Imports
System.Drawing
Imports
System.Data.SqlClient
Imports
System.Data
Partial
Class
Templet_css_GridView
Inherits
System.Web.UI.Page
Dim
conn
As
New
SqlConnection(
"Path"
)
Dim
cmd
As
New
SqlCommand()
Protected
Sub
Button1_Click(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Button1.Click
Response.ClearContent()
Response.AddHeader(
"content-disposition"
,
"attachment; filename=gvtoexcel.xls"
)
Response.ContentType =
"application/excel"
Dim
sw
As
New
System.IO.StringWriter()
Dim
htw
As
New
HtmlTextWriter(sw)
GridView1.RenderControl(htw)
Response.Write(sw.ToString())
Response.[
End
]()
DropDownList1.SelectedItem.ToString()
End
Sub
Public
Overrides
Sub
VerifyRenderingInServerForm(
ByVal
control
As
Control)
'Tell the compiler that the control is rendered
'explicitly by overriding the VerifyRenderingInServerForm event.
End
Sub
Protected
Sub
Page_Load(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
Handles
Me
.Load
Dim
adapter
As
New
SqlDataAdapter()
Dim
ds
As
New
DataSet()
Dim
i
As
Integer
= 0
Dim
sql
As
String
=
Nothing
Dim
connetionString
As
String
=
"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\manish\WebSite\App_Data\mani.mdf;Integrated Security=True;User Instance=True"
sql =
"select name,roll,monumber,coll,class from my"
Dim
connection
As
New
SqlConnection(connetionString)
connection.Open()
Dim
command
As
New
SqlCommand(sql, connection)
adapter.SelectCommand = command
adapter.Fill(ds)
adapter.Dispose()
command.Dispose()
connection.Close()
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
DropDownList1.DataSource = ds.Tables(0)
DropDownList1.DataTextField =
"name"
DropDownList1.DataBind()
'TextBox1.Text = "fffffffff"
End
Sub
Save gridview data in excel
Save GridView Data
ASP.NET GridView
Save data to Excel