Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

GridView Export to Excel

Posted by Viral Sarvaiya on July 16, 2009

Exporting GridView to Excel is a very common task which is performed in most of the web applications. There are various techniques of exporting the GridView to excel and it highly depends on the application scenario. In this article I will demonstrate some techniques that you will find useful.

Exporting GridView to Excel is a very common task which is performed in most of the web applications. There are various techniques of exporting the GridView to excel and it highly depends on the application scenario. In this article I will demonstrate some techniques that you will find useful.

Response.Clear()

Response.Buffer = True

Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls")

Response.Charset = ""

Response.ContentType = "application/vnd.ms-excel"

Dim sw As New StringWriter()

Dim hw As New HtmlTextWriter(sw)

GridView1.DataBind()

GridView1.RenderControl(hw)

Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"

Response.Write(style)

Response.Output.Write(sw.ToString())

Response.Flush()

Response.End()

this code gives you the open/save/cancel dialog box of the excel file

if you want to save directly to the server then write the below code.

Dim sw As New StringWriter()

Dim hw As New HtmlTextWriter(sw)

GridView1.DataBind()

GridView1.RenderControl(hw)

Dim style As String = "<style>.textmode{mso-number-format:\@;}</style>"

System.IO.File.WriteAllText("F:\\Test.xls", style & sw.ToString())

with this code the file of the excel is automatically stored in the particular folder which u provide.

Advertisements

2 Responses to “GridView Export to Excel”

  1. Sunil Raizada said

    Dear viral the above code of saving the excel file directly to server is good for the limited records.
    when we trying to save e.g. 10000 records in the excel sheet then it gives just 1000,
    and this excel file takes very much time to open.
    so if u have any solution the give me a reply.

  2. […] From my past posts explains how to export to excel as 1. Export GridView Data into CSVFile In Asp.net 2. GridView Export to Excel […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: