Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Export data in Excel from Gridview in VB.NET
WhatsApp
Mukesh Kumar
Oct 05
2015
18.7
k
0
1
Export code
Protected
Sub
ExportToExcel(sender
As
Object
, e
As
EventArgs)
Response.Clear()
Response.Buffer =
True
Response.AddHeader(
"content-disposition"
,
"attachment;filename=GridViewExport.xls"
)
Response.Charset =
""
Response.ContentType =
"application/vnd.ms-excel"
Using sw
As
New
StringWriter()
Dim
hw
As
New
HtmlTextWriter(sw)
'To Export all pages
GridView1.AllowPaging =
False
Me
.BindGrid()
GridView1.HeaderRow.BackColor = Color.White
For
Each
cell
As
TableCell
In
GridView1.HeaderRow.Cells
cell.BackColor = GridView1.HeaderStyle.BackColor
Next
For
Each
row
As
GridViewRow
In
GridView1.Rows
row.BackColor = Color.White
For
Each
cell
As
TableCell
In
row.Cells
If
row.RowIndex
Mod
2 = 0
Then
cell.BackColor = GridView1.AlternatingRowStyle.BackColor
Else
cell.BackColor = GridView1.RowStyle.BackColor
End
If
cell.CssClass =
"textmode"
Next
Next
GridView1.RenderControl(hw)
'style to format numbers to string
Dim
style
As
String
=
"<style> .textmode { } </style>"
Response.Write(style)
Response.Output.Write(sw.ToString())
Response.Flush()
Response.[
End
]()
End
Using
End
Sub
Public
Overrides
Sub
VerifyRenderingInServerForm(control
As
Control)
' Verifies that the control is rendered
End
Sub
vb.net
Gridview
Export data in Excel
Up Next
Export data in Excel from Gridview in VB.NET