473,386 Members | 1,841 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,386 software developers and data experts.

Data Set to Excel ?

Rob
Is there an easy (and fast) way to transfer the contents of a dataset to
Excel (without using the Excel object ) ?
Oct 3 '06 #1
6 1355
Excel supports xml as if they were xls documents. There are only a couple
of nuances to be concerned with.

1. Processor Instrcutions
2. Additional Hierachy Members

Save an Excel Document as XML, open it in notepad and replicate the layout.
I think it is pretty cool.

Of course you can always save the dataset as a CSV, Ecxel also knows this
format real well.


"Rob" <rw*****@comcast.netwrote in message
news:yo******************************@comcast.com. ..
Is there an easy (and fast) way to transfer the contents of a dataset to
Excel (without using the Excel object ) ?

Oct 3 '06 #2
Is there a fast way....sure (streamwriter), is it easy....everything is
easy once you know how to do it.

You could write a routine to loop through evey table and every row and
every column and export all of it too a .csv file. Then you could open
that in Excel.

I have a need to display data in a certain way, formated a certain way
in excel and for this I created a Crystal Report and then exported it
too Excel.

I think I already have a progam that does this, it run a query against
a database then exports the resuts to a .csv file.

Let me know if you want the code.

Izzy

BTW, if you want super quick and easy and don't care about formatting
at all.

DataSet.WriteXml(Path)

Then you could read the .xml file in Excel.
Rob wrote:
Is there an easy (and fast) way to transfer the contents of a dataset to
Excel (without using the Excel object ) ?
Oct 3 '06 #3
Rob
Thanks Izzy,
I think I already have a progam that does this, it run a query against
a database then exports the resuts to a .csv file.
If you have the code handy, that would be great...

Thanks,
Rob
"Izzy" <is************@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
Is there a fast way....sure (streamwriter), is it easy....everything is
easy once you know how to do it.

You could write a routine to loop through evey table and every row and
every column and export all of it too a .csv file. Then you could open
that in Excel.

I have a need to display data in a certain way, formated a certain way
in excel and for this I created a Crystal Report and then exported it
too Excel.

I think I already have a progam that does this, it run a query against
a database then exports the resuts to a .csv file.

Let me know if you want the code.

Izzy

BTW, if you want super quick and easy and don't care about formatting
at all.

DataSet.WriteXml(Path)

Then you could read the .xml file in Excel.
Rob wrote:
>Is there an easy (and fast) way to transfer the contents of a dataset to
Excel (without using the Excel object ) ?

Oct 3 '06 #4
BK
If you have the code handy, that would be great...
>
Thanks,
Rob
This is code we have in one of our base forms for dumping the contents
of a datagrid to Excel. It works with the old style datagrids (.NET
FW1.1, VS 2003) and will work in FW 2.0. I don't know if it works with
the new datagrid object, but I'm sure it could be adapted. Hope this
helps:

Me.Cursor = Cursors.WaitCursor
Dim Excel As New Microsoft.Office.Interop.Excel.Application
Dim lcMappingName As String
Dim excelColumn As Integer, excelRow As Integer, colCtr As
Integer
excelColumn = 0
excelRow = 1
Excel.Application.Workbooks.Add(True)
Dim rowIndex As Integer
Dim GridTextColumn As DataGridTextBoxColumn
Dim GridBoolColumn As DataGridBoolColumn
For colCtr = 0 To
DataGrid1.TableStyles(0).GridColumnStyles.Count - 1
If
DataGrid1.TableStyles(0).GridColumnStyles(colCtr). GetType.ToString() =
"System.Windows.Forms.DataGridTextBoxColumn" Then
GridTextColumn =
DataGrid1.TableStyles(0).GridColumnStyles(colCtr)
lcMappingName = GridTextColumn.MappingName
Else
GridBoolColumn =
DataGrid1.TableStyles(0).GridColumnStyles(colCtr)
lcMappingName = GridBoolColumn.MappingName
End If
If
DataGrid1.TableStyles(0).GridColumnStyles.Item(lcM appingName).Width() >
0 Then
excelColumn += 1
Excel.Cells(1, excelColumn) =
DataGrid1.TableStyles(0).GridColumnStyles.Item(lcM appingName).HeaderText
rowIndex = 2
Dim row As DataRow
For Each row In DataSet1.Tables(0).Rows
rowIndex += 1
Excel.Cells(rowIndex, excelColumn) =
row(lcMappingName).ToString()
Next row
End If
Next

Me.Cursor = Cursors.Default
Excel.Visible = True

Oct 3 '06 #5
Here you go, pass your DataSet to this function and it will create a
..csv file out of it. It also has an optional parameter to export the
column names.

It will prompt you to save it somewhere.

Enjoy.

Private Sub WriteToCSV(ByVal dsData As DataSet, Optional ByVal
ExportColumnNames As Boolean = False)

Dim dtTable As DataTable
Dim drRow As DataRow
Dim strData As String = ""
Dim swWriter As IO.StreamWriter
Dim dcColumn As DataColumn
Dim sfdFile As SaveFileDialog
Dim i As Int32 = 0

Try

For Each dtTable In dsData.Tables

strData = strData & dtTable.TableName & vbCrLf

If ExportColumnNames Then

For Each dcColumn In dtTable.Columns
strData = strData & dcColumn.ColumnName & ","
Next

strData = strData.TrimEnd(",") & vbCrLf
End If

For Each drRow In dtTable.Rows

For i = 0 To dtTable.Columns.Count - 1
strData = strData &
drRow(i).ToString.Trim.Replace(",", "") & ","
Next

strData = strData.TrimEnd(",") & vbCrLf
Next

strData = strData & vbCrLf
Next

If strData.Length 0 Then

sfdFile = New SaveFileDialog

With sfdFile
.Filter = "CSV Files (*.csv)|*.csv"
.ShowDialog()
End With

If Not sfdFile.FileName.Length = 0 Then
swWriter = New IO.StreamWriter(sfdFile.FileName)
swWriter.WriteLine(strData)
swWriter.Close()
swWriter.Dispose()
End If

sfdFile.Dispose()
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly)
End Try

End Sub
Izzy
Rob wrote:
Thanks Izzy,
I think I already have a progam that does this, it run a query against
a database then exports the resuts to a .csv file.

If you have the code handy, that would be great...

Thanks,
Rob
"Izzy" <is************@gmail.comwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
Is there a fast way....sure (streamwriter), is it easy....everything is
easy once you know how to do it.

You could write a routine to loop through evey table and every row and
every column and export all of it too a .csv file. Then you could open
that in Excel.

I have a need to display data in a certain way, formated a certain way
in excel and for this I created a Crystal Report and then exported it
too Excel.

I think I already have a progam that does this, it run a query against
a database then exports the resuts to a .csv file.

Let me know if you want the code.

Izzy

BTW, if you want super quick and easy and don't care about formatting
at all.

DataSet.WriteXml(Path)

Then you could read the .xml file in Excel.
Rob wrote:
Is there an easy (and fast) way to transfer the contents of a dataset to
Excel (without using the Excel object ) ?
Oct 3 '06 #6
you can bind the datset to the gridview and then export it in the following way.

With Response
.Clear()
.AddHeader("content-disposition", "attachment;filename=" + strFileName)
.Cache.SetCacheability(HttpCacheability.NoCache)
.ContentType = "application/vnd.ms-excel"
End With

Dim strWrite As System.IO.StringWriter = New System.IO.StringWriter
Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(strWrite)

Dim frm As HtmlForm = New HtmlForm
Me.Controls.Add(frm)
frm.Controls.Add(ExportGridView)
frm.RenderControl(htmlWrite)
Response.Write(strWrite.ToString)
Response.End()

EggHeadCafe.com - .NET Developer Portal of Choice
http://www.eggheadcafe.com
Oct 6 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: RK | last post by:
Hi, In my application, I need to copy data from an Excel file into a SQL table. The article related to this can be found at http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B306572 ...
3
by: Chris | last post by:
Could someone please provide me an effective means of exporting data from a data set (or data grid) to Excel?
2
by: Niyazi | last post by:
Hi, I have to retrieve a data from AS400 DB2 and after working with data I have to export into one of existing Excel file. I can connect into specific library in AS400 DB2 using AS400...
1
by: Steven Stewart | last post by:
I have a user who has been using Excel for a while to keep statistics and print reports. She finds using it cumbersome because of long formulas and a lot of copying and pasting. I have designed...
3
by: Mads Petersen | last post by:
I'm stuck in this code. Hope you can and will help me. I launch it from excel. I have made the following code work, but not as i whant. I need the ranges to be working with something like xlDown....
11
by: Hi5 | last post by:
Hi, I am new to access I usedto work in Oracle and Mysql. I am after a way that enables me to populate a database I designed in access with lots of data which can be sorted in excel sheets, ...
0
by: Grip | last post by:
Hi, I have gone throught the group and Microsoft's online help and have seen many suggestions but I am still seeking clarity: 1. I have an excel spreadsheet. Column A contains text that may...
7
by: semijoyful | last post by:
OS: Win XP SP2 Access version: 2003 Excel version: 2003 I am new at this, as I am sure you have gathered from this post title:) I am working on a form where users can input data in Access and...
7
Merlin1857
by: Merlin1857 | last post by:
Its great producing data for users to look at in your web pages and generally that is sufficient for their needs but sometimes you may want to supply your user with the data in a form they can...
3
by: =?Utf-8?B?YzY3NjIyOA==?= | last post by:
Hi all, I have a question for you. I have a .csv file which has many lines of data. Each line has many data fields which are delimited by ",". Now I need to extract part of data from this...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.