Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

How to export data of DataGrid Excel file using VB.Net.
Download Source Code :

Following code snippet shows you how you can export data, present in a DataGrid, to an Excel file.

  1. Response.Clear()

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

3.      Response.Charset = ""

4.      Response.Cache.SetCacheability(HttpCacheability.NoCache)

5.      Response.ContentType = "application/vnd.xls"

6.      Dim stringWrite As new System.IO.StringWriter()

7.      Dim htmlWrite As new HtmlTextWriter(stringWrite)

8.      myDataGrid.RenderControl(htmlWrite)

9.      Response.Write(stringWrite.ToString())



  • The Response.Clear method erases any buffered HTML output. However, the Response.Clear method erases only the response body; it does not erase response headers. You can use this method to handle error cases. Note that this method causes a run-time error if Response.Buffer has not been set to TRUE.
  • The Response.AddHeader method tells the server what type of file is being exported i.e.”FileName.xls”
  • The Response.Charset method specifies the name of the character set.
  • The Response.Cache.SetCacheability method sets or removes the Caching
  • The Response.ContentType method specifies what type of file is being exported.
  • The Response.RenderControl method writes the data to HTML text writer.
  • The Response.Write method writes to the file
Article Comments
The code shown above was written in, if i'm using VB Express, is there any way to export the datagrid to excel?


Posted on 6/5/2006 12:09:58 PM by CK Ong

WOW, nice, very nice mate.

Posted on 1/21/2007 6:30:17 PM by Dimitrios A. Tzortzis

I read your article "How to export data of DataGrid Excel file using VB.Net." really it is nice
but i face below problem

I tried using the example for excel, and can successfully
"save" the spreadsheet to my machine,but the "open" option fails with this error:
"... \Temporary Internet Files\content.IE5\A5GR6p25\POWERbase.xls[1].xls' could not be found.
Check the spelling... If you are trying to open the file from your list of most recently
used files on the file menu, make sur that the file has not been renamed, moved, or deleted."

help, please???

Regards & Thanks


Posted on 1/24/2007 2:19:04 AM by Mukesh

Hi..I tried through this coding.
But It is giving a runtime error on Code line 8
as "myDataGrid_ctr10_ctl1.. "DatagridLink" should be inside a "from" tag..
How can I taggle this problem.. Please send me the solution..
The code I tried is
1. Response.Clear()

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

3. Response.Charset = ""

4. Response.Cache.SetCacheability(HttpCacheability.NoCache)

5. Response.ContentType = "application/vnd.xls"

6. Dim stringWrite As new System.IO.StringWriter()

7. Dim htmlWrite As new HtmlTextWriter(stringWrite)

8. myDataGrid.RenderControl(htmlWrite)

9. Response.Write(stringWrite.ToString())

Joe Babu

Posted on 2/8/2007 12:37:06 AM by Joe Babu

i want coding to export datagrid toexcel file format

Posted on 3/19/2007 2:21:06 AM by karunakaran

How to export Datas from Grid to Excel sheet in vb6

Posted on 4/16/2007 3:49:28 AM by Thanu

export data from datagrid to excel file

Posted on 4/29/2007 2:39:03 AM by nagabhushanam9

Good easy to understand.
no extra efforts needed

Posted on 5/1/2007 6:02:35 AM by Pallavi

Excellent article

Posted on 5/27/2007 2:59:20 AM by seshadri

please, can u sent me how to export grid data to excel in desktop application using

Posted on 2/13/2008 1:42:05 AM by Joseph

Here is an article that shows how to import/export to XLSX (Excel 2007) files:

Posted on 6/13/2008 9:11:37 AM by Mario

i used the above code to export the data into the excel sheet, it worked out well, but when opened the excel sheet the all the grid lines are not visible in the excel sheet only data grid lines are visible
could anyone help me out in solving this


Posted on 2/11/2009 4:07:23 AM by srikanth

there is an error for response.wat name space should be added?

Posted on 2/24/2010 1:54:57 AM by Sruthi

I am using your code in c# in,but while opening excel file in excel sheet,it shows message box that" file extension is differtnt that defined or file is currepted".
please tell me solution for this.

Posted on 3/25/2010 10:50:45 AM by Nikhil

Add Article Comment:
Name :
Email Address :
Comments :
<< How to rewrite URL in Global.asax file using VB.Net.

Disclaimer - Privacy
© 2002-2017