| | |
Export DataTable to Excel with variable number of fields
Please support our VB.NET advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2006
Posts: 62
Reputation:
Solved Threads: 3
Hi!
Sorry about my bad grammer and/or spelling. English is not my native language.
I don't know if this already have been asked and answered, however...
I'm currently developing a program in which it will be possible to select table fields from a database and set various criteria using dropdown-lists. This will then dynamically generate an SQL-query string.
I then convert the datareader I use for this query into a datatable.
The above part works great.
Now to the problem.
I have other functions that perform an export to excel, but those has fixed number of fields and it's then easy, for example, to use
But because this latest function has fields that varies in number, the above code won't exactly work.
I need a way to retrieve the information into a single string where:
And here's the crux. All this has to be performed inside a single
Any ideas?
Sorry about my bad grammer and/or spelling. English is not my native language.
I don't know if this already have been asked and answered, however...
I'm currently developing a program in which it will be possible to select table fields from a database and set various criteria using dropdown-lists. This will then dynamically generate an SQL-query string.
I then convert the datareader I use for this query into a datatable.
The above part works great.
Now to the problem.
I have other functions that perform an export to excel, but those has fixed number of fields and it's then easy, for example, to use
VALUES ('" & row.Item(0) & "','" & row.Item(2) & "','" & row.Item(3) & "'..... to retrieve and export the information.But because this latest function has fields that varies in number, the above code won't exactly work.
I need a way to retrieve the information into a single string where:
str &= "'" & row.Item(<dynamic number 0 to field.count - 1>) & "'," .And here's the crux. All this has to be performed inside a single
For Each row AS DataRow In DataTable.Rows because the data is exported into excel row by row.Any ideas?
Last edited by Oxiegen; Dec 4th, 2007 at 10:51 am.
•
•
Join Date: Jun 2006
Posts: 62
Reputation:
Solved Threads: 3
Problem solved.
I simply added a funktion that returns the string containing the values.
By calling the funktion like this:
The information is returned like this:
I simply added a funktion that returns the string containing the values.
By calling the funktion like this:
VB.NET Syntax (Toggle Plain Text)
values = DataValues(row, dt.Columns.Count - 1)
VB.NET Syntax (Toggle Plain Text)
Private Function DataValues(ByVal DRow As DataRow, ByVal Max As Integer) As String Dim retVal As String retVal = "'" & DRow.Item(0) & "'," For i As Integer = 1 To Max - 1 retVal &= "'" & DRow.Item(i) & "'," Next retVal &= "'" & DRow.Item(Max) & "'" Return retVal End Function
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: Printing HTML from VB.Net
- Next Thread: Code generator that produces SQL Stored Procedures
Views: 7134 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for VB.NET
"crystal .net .net2005 2008 access add advanced application array assignment basic box button buttons center click code combo convert cpu data database datagrid datagridview dissertation dissertations dissertationthesis dosconsolevb.net editvb.net employees error excel exists firewall function image images isnumericfuntioncall listview login map math memory mobile module msaccess mssqlbackend mysql navigate net number opacity page pan picturebox picturebox2 port print printing printpreview problem record regex reports" reuse right-to-left save savedialog search serial socket sorting sql sqldatbase storedprocedure string structures studio temp textbox timer txttoxmlconverter upload useraccounts usercontol usercontrol vb vb.net vb.nettoolboxvisualbasic2008sidebar vb2008 vbnet vista visual visualbasic visualbasic.net visualstudio2008 web winsock wpf xml





