22
May

Export Gridview Data to Excel Sheet in Asp .net

   Posted by: admin   in .Net Emperor

Hello Friends

Few days ago one of my friend told me please tell me how can i export Gridview`s data into the Excel sheet he needed in his working project.

so i m writing this post from them who need same stuff in their projects.

Create one button for the Export Excel Functionality and it`s Click event do following code.

Protected Sub btnExport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExport.Click
Response.Clear()
Response.AddHeader(”content-disposition”, “attachment;filename=test.xls”)
Response.ContentType = “application/vnd.xls”

Dim sw As StringWriter = New StringWriter()
Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)

GridView1.RenderControl(htw)

Response.Write(sw.ToString())
Response.End()
End Sub

After Create Button Code just copy and paste below code

on page

Public Overrides Sub VerifyRenderingInServerForm(ByVal control As Control)

End Sub

last change is

<%@ Page Language="VB" AutoEventWireup="true" enableEventValidation =”false” CodeFile=”Default.aspx.vb” Inherits=”_Default” %>

Make enableEventValidation =”false” on page directives.

Please post comment that code works for you or not

Thanks In Advance.

Tags: ,

This entry was posted on Friday, May 22nd, 2009 at 1:55 am and is filed under .Net Emperor. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment