473,396 Members | 2,014 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Export to Excel with DataGrid in VB.NET 2003?

Hello everyone.

I am trying to export the contents of a DataGrid to MS Excel using VB.NET 2003. I read of a couple different ways of doing that here (though I couldn't tell which version of .NET was the subject of the article):
http://www.aspnetpro.com/NewsletterArticle/2003/09/asp200309so_l/asp200309so_l.asp

The following code snippet from that article DOES work for me:

-----------------------------------------------------------------------------
'Converts a DataTable to Tab Delimited Format
Private Function ConvertDtToTDF(ByVal dt As DataTable) As String

Dim dr As DataRow, ary() As Object, i As Integer
Dim iCol As Integer

'Output Column Headers
For iCol = 0 To dt.Columns.Count - 1
Response.Write(dt.Columns(iCol).ToString & vbTab)
Next
Response.Write(vbCrLf)

'Output Data
For Each dr In dt.Rows
ary = dr.ItemArray
For i = 0 To UBound(ary)
Response.Write(ary(i).ToString & vbTab)
Next
Response.Write(vbCrLf)
Next

End Function
-----------------------------------------------------------------------------

But what I really want to do is to export HTML, not just in tab delimited format. The following code snippet from that article DOES NOT work for me.

-----------------------------------------------------------------------------
Protected Overrides Sub Render(ByVal hWriter As System.Web.UI.HtmlTextWriter)

'Create and populate a DataTable
Dim dt As DataTable = New DataTable()
dt = MakeTable(dt)

Response.ContentType = "application/ms-excel"
Response.AddHeader("Content-Disposition", "inline;filename=Process_Aspect_List_By_Process.xl s")

DataGrid1.DataSource = dt
DataGrid1.DataBind()
DataGrid1.RenderControl(hWriter)

End Sub
-----------------------------------------------------------------------------

A blank page or blank excel spreadsheet (depending on wheither I define the Response.AddHeader with 'inline' or 'attachment') appears when the latter code is run.

I have tried every version and permutation of this idea that I can find on the web, but none of them working. Anyone have any ideas on why it isn't working or what I might need to change?

Much appreciated!
-Christine
Jul 2 '07 #1
1 2749
UPDATE:

Turns out I just had the html goofed up on the actual page so it wasn't being written correctly. After creating a table to house the datagrid, the export did indeed work.

-Christine
Jul 2 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Maria L. | last post by:
Hi, I need to export the content of a DataGrid (in Windows application in C#), into an Excel spreadsheet. Anyone knows how to do this? Any code snippets would help! thanks a lot, Maria
9
by: | last post by:
I have a web page written in asp.net that has multiple datagrids on it that would need to be exported to Excel. Each of the datagrids would be a subset of what the datagrid above it was. Thus...
1
by: ad | last post by:
I used the code below to export a table in a dataset to Excel. It can export only on table at a time. Can export more than on table in a dataset to Excel public static void...
1
by: Diego | last post by:
Hi, I have a ASP.NET application and i'm interested in reporting in excel format. I read a good artile written by Steve C. Orr that gave me info about exporting to excel. ...
8
by: DC Gringo | last post by:
I have a simple button that should open another window and export a datagrid to an Excel file. I'm getting: "Name 'window' is not declared." What do I need to declare or import? <INPUT...
3
by: mattdaddym | last post by:
Hi all, I have been using the following code to export my datagrids to excel for quite some time. Very simple and very effective. <code> Sub btnExcelExport_Click ( s As Object, e As...
6
by: Sridhar | last post by:
Hi, I have to export data inside the datagrid into an excel worksheet. I have to do this on button click event. I am trying to do like this. First get the DataTable from the datagrid and...
4
by: Frank | last post by:
Hello All, I ham using VS.NET 2003. Have followed instructions from http://gridviewguy.com/ArticleDetails.aspx?articleID=26 along with several other articles to no avail. I am pulling my hair...
1
by: elangovan.cse | last post by:
Hi all, I'm exporting a datagrid to an excel file. I'm using the following source code to export. DataTable dt = new DataTable(); DataAccess.StoredProcedure sproc = new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.