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

HELP - How to Dump a Dataset into Excel???

Ed
Hi All,

I'm looking for an efficient way of dumping a table in a Dataset into
an Excel sheet. I'm currently copying data cell by cell and this is
rather inefficient for large Datasets.

My current code is as follows:

objExcel = New Excel.Application()
objWorkBooks = objExcel.Workbooks
objWorkBook = objWorkBooks.Add
objWorkSheet = objExcel.ActiveWorkbook.ActiveSheet
objWorkSheet.Name = sTitle
objExcel.Visible = True

'Column headers
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(1, iColIndex) = dcCol.ColumnName
objWorkSheet.Cells(1, iColIndex).Font.Bold = True
Next dcCol

'Now put in the actual data
'*******************************************
'THIS IS THE PART THAT TAKES A LONG TIME
'*******************************************
iRowIndex = 1
For Each drRow In dsExport.Tables(0).Rows
iRowIndex += 1
iColIndex = 0
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(iRowIndex, iColIndex) =
drRow(dcCol.ColumnName).ToString()
Next dcCol
Next drRow

'Autofit the cells
objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit()
Does anyone out there know how I can improve this? And I don't want
to use the XML method.

Many Thanks!!!
Nov 20 '05 #1
12 2325
I believe what you need is
http://www.dotnetjohn.com/articles/articleid36.aspx

this !! :)

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/

"Ed" <le****@hotmail.com> wrote in message
news:cf**************************@posting.google.c om...
Hi All,

I'm looking for an efficient way of dumping a table in a Dataset into
an Excel sheet. I'm currently copying data cell by cell and this is
rather inefficient for large Datasets.

My current code is as follows:

objExcel = New Excel.Application()
objWorkBooks = objExcel.Workbooks
objWorkBook = objWorkBooks.Add
objWorkSheet = objExcel.ActiveWorkbook.ActiveSheet
objWorkSheet.Name = sTitle
objExcel.Visible = True

'Column headers
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(1, iColIndex) = dcCol.ColumnName
objWorkSheet.Cells(1, iColIndex).Font.Bold = True
Next dcCol

'Now put in the actual data
'*******************************************
'THIS IS THE PART THAT TAKES A LONG TIME
'*******************************************
iRowIndex = 1
For Each drRow In dsExport.Tables(0).Rows
iRowIndex += 1
iColIndex = 0
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(iRowIndex, iColIndex) =
drRow(dcCol.ColumnName).ToString()
Next dcCol
Next drRow

'Autofit the cells
objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit()
Does anyone out there know how I can improve this? And I don't want
to use the XML method.

Many Thanks!!!

Nov 20 '05 #2
Ok another idea is, Excel 2003, can directly read or save to XML .. you
could use that facility instead.

- Sahil Malik
Independent Consultant
You can reach me thru my blog - http://dotnetjunkies.com/WebLog/sahilmalik/

"Ed" <le****@hotmail.com> wrote in message
news:cf**************************@posting.google.c om...
Hi All,

I'm looking for an efficient way of dumping a table in a Dataset into
an Excel sheet. I'm currently copying data cell by cell and this is
rather inefficient for large Datasets.

My current code is as follows:

objExcel = New Excel.Application()
objWorkBooks = objExcel.Workbooks
objWorkBook = objWorkBooks.Add
objWorkSheet = objExcel.ActiveWorkbook.ActiveSheet
objWorkSheet.Name = sTitle
objExcel.Visible = True

'Column headers
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(1, iColIndex) = dcCol.ColumnName
objWorkSheet.Cells(1, iColIndex).Font.Bold = True
Next dcCol

'Now put in the actual data
'*******************************************
'THIS IS THE PART THAT TAKES A LONG TIME
'*******************************************
iRowIndex = 1
For Each drRow In dsExport.Tables(0).Rows
iRowIndex += 1
iColIndex = 0
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(iRowIndex, iColIndex) =
drRow(dcCol.ColumnName).ToString()
Next dcCol
Next drRow

'Autofit the cells
objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit()
Does anyone out there know how I can improve this? And I don't want
to use the XML method.

Many Thanks!!!

Nov 20 '05 #3
If you are using Excel2000 or higher you can use a com ADO
inside Excel, if the source data comes from say Sql Server
or Access.

Dim RS As New ADODB.Recordset, conn As New ADODB.Connection
Set conn = ...
RS.Open...
Sheet1.Range("A1").CopyFromRecordset RS

If using Excel97 you can still use com ADO except that 97
doesn't support ADO and thus can't use

Sheet1.Range("A1").CopyFromRecordset RS

Have to use a Range object to write the data from the
recordset variable to the sheet.

If the source data is not from Sql Server (or something
you can connect to) then you can use Com ADO in your
vb.net app and write the data to Excel:

RS.CursorLocation = adUseClient
conn.Mode = adModeReadWrite
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\test1.xls" & _
"Extended Properties=""Excel 9.0;HDR=NO;"""
For j = 0 to dt.Rows.Count -1
dr = dt.Rows(j)
strSql = "SELECT * FROM [Sheet1$A" & j + 1 & ":M" & j + 1
& "]"
RS.Open strSql, cn, adOpenDynamic, adLockPessimistic
For i = 0 to RS.Fields.Count - 1 : RS(i) = dr(i) : Next
RS.Update
RS.close
Next

Note: writing to Excel using com ADO (very fast) can only
do one row at a time. j will define each row in your
dataTable and each row in Excel. Here you will write to a
row in Excel from Range("A" & j + 1 & ":M" & j + 1).
Excel Rows start at 1. So you will have 2 loops, one for
each row and one for each column. With this method you
don't even have to make a reference to the Excel Object
library from your project.

Rich
-----Original Message-----
Hi All,

I'm looking for an efficient way of dumping a table in a Dataset intoan Excel sheet. I'm currently copying data cell by cell and this israther inefficient for large Datasets.

My current code is as follows:

objExcel = New Excel.Application()
objWorkBooks = objExcel.Workbooks
objWorkBook = objWorkBooks.Add
objWorkSheet = objExcel.ActiveWorkbook.ActiveSheet
objWorkSheet.Name = sTitle
objExcel.Visible = True

'Column headers
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(1, iColIndex) = dcCol.ColumnName
objWorkSheet.Cells(1, iColIndex).Font.Bold = True
Next dcCol

'Now put in the actual data
'*******************************************
'THIS IS THE PART THAT TAKES A LONG TIME
'*******************************************
iRowIndex = 1
For Each drRow In dsExport.Tables(0).Rows
iRowIndex += 1
iColIndex = 0
For Each dcCol In dsExport.Tables(0).Columns
iColIndex += 1
objWorkSheet.Cells(iRowIndex, iColIndex) =
drRow(dcCol.ColumnName).ToString()
Next dcCol
Next drRow

'Autofit the cells
objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit()
Does anyone out there know how I can improve this? And I don't wantto use the XML method.

Many Thanks!!!
.

Nov 20 '05 #4
Hi Ed,

It is very simple and the sample is here.

http://msdn.microsoft.com/library/de...ntodataset.asp

I hope this helps?

Cor
Nov 20 '05 #5
Hi Rich,

Is this not the answer for a Recordset?

Cor

Dim RS As New ADODB.Recordset, conn As New ADODB.Connection
Set conn = ...
RS.Open...
Sheet1.Range("A1").CopyFromRecordset RS

If using Excel97 you can still use com ADO except that 97
doesn't support ADO and thus can't use

Sheet1.Range("A1").CopyFromRecordset RS

Have to use a Range object to write the data from the
recordset variable to the sheet.

If the source data is not from Sql Server (or something
you can connect to) then you can use Com ADO in your
vb.net app and write the data to Excel:

RS.CursorLocation = adUseClient
conn.Mode = adModeReadWrite
conn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\test1.xls" & _
"Extended Properties=""Excel 9.0;HDR=NO;"""
For j = 0 to dt.Rows.Count -1
dr = dt.Rows(j)
strSql = "SELECT * FROM [Sheet1$A" & j + 1 & ":M" & j + 1
& "]"
RS.Open strSql, cn, adOpenDynamic, adLockPessimistic
For i = 0 to RS.Fields.Count - 1 : RS(i) = dr(i) : Next
RS.Update
RS.close
Next

Note: writing to Excel using com ADO (very fast) can only
do one row at a time. j will define each row in your
dataTable and each row in Excel. Here you will write to a
row in Excel from Range("A" & j + 1 & ":M" & j + 1).
Excel Rows start at 1. So you will have 2 loops, one for
each row and one for each column. With this method you
don't even have to make a reference to the Excel Object
library from your project.

Nov 20 '05 #6
http://www.kjmsolutions.com/datasetarray.htm

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl:
Hi Ed,

It is very simple and the sample is here.
http://msdn.microsoft.com/library/de...us/dv_vbcode/h
tml/vbtskcodeexamplereadingexceldataintodataset.asp

I hope this helps?

Cor


Nov 20 '05 #7
Hi Kim,

I see it now, I was as well reading it wrong.
Dumping a table in a dataset .............................. in an Excel
sheet and that I did not read as it was.

Otherwise I had given your link.

:-)

Cor
Nov 20 '05 #8
My name is "Kelly"...

:)

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl:
Hi Kim,

I see it now, I was as well reading it wrong.
Dumping a table in a dataset .............................. in an Excel
sheet and that I did not read as it was.

Otherwise I had given your link.

:-)

Cor


Nov 20 '05 #9
Hi Kelly,

I knew that there was a posibillity that you would write this KJM.

I saw it to late,

Sorry

:-)

Cor
My name is "Kelly"...


Nov 20 '05 #10
Ed
Thanks for all your responses... I tried the example and I keep
getting the error message:
"Exception from HRESULT: 0x800A03EC."

Whenever this line executes:
objWorkSheet.Range("A1").Resize(iRowCount, iColumnCount).Value =
DataArray

Any ideas?
ed

scorpion53061 <ad***@nospampleasekjmsolutions.com> wrote in message news:<ui**************@TK2MSFTNGP12.phx.gbl>...
http://www.kjmsolutions.com/datasetarray.htm

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl:
Hi Ed,

It is very simple and the sample is here.
http://msdn.microsoft.com/library/de...us/dv_vbcode/h
tml/vbtskcodeexamplereadingexceldataintodataset.asp

I hope this helps?

Cor

Nov 20 '05 #11
Post the code you are using and an attachment with sample data.

I will run it through for you.

"Ed" <le****@hotmail.com> wrote in message
news:cf*************************@posting.google.co m:
Thanks for all your responses... I tried the example and I keep
getting the error message:
"Exception from HRESULT: 0x800A03EC."

Whenever this line executes:
objWorkSheet.Range("A1").Resize(iRowCount, iColumnCount).Value =
DataArray

Any ideas?
ed

scorpion53061 <ad***@nospampleasekjmsolutions.com> wrote in message
news:<ui**************@TK2MSFTNGP12.phx.gbl>...
http://www.kjmsolutions.com/datasetarray.htm

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:em**************@TK2MSFTNGP10.phx.gbl:
Hi Ed,

It is very simple and the sample is here.

http://msdn.microsoft.com/library/de...us/dv_vbcode/h

tml/vbtskcodeexamplereadingexceldataintodataset.asp

I hope this helps?

Cor


Nov 20 '05 #12
No problem. It gave me a good laugh this morning.... :)

"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ez*************@TK2MSFTNGP09.phx.gbl:
Hi Kelly,

I knew that there was a posibillity that you would write this KJM.

I saw it to late,

Sorry

:-)

Cor
My name is "Kelly"...


Nov 20 '05 #13

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

Similar topics

8
by: Ed | last post by:
Hi All, I'm looking for an efficient way of dumping a table in a Dataset into an Excel sheet. I'm currently copying data cell by cell and this is rather inefficient for large Datasets. My...
9
by: Paul | last post by:
Hi all Arggghhh........... The problem.....I want the user to be able to create an excel document and name particular cells in the document where they want the data to be placed and then save...
2
by: Eric | last post by:
Hi, I am making a flash card type program for the PocketPC. I am using a dataset to contain the cards, all of that works great. I thought I would just import the XML into word or excel,...
3
by: JacksonYin | last post by:
1. I can fill data from Excel to DataSet like this: OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../../Book1.xls;Extended Properties=Excel...
4
by: Hans [DiaGraphIT] | last post by:
Hi! I want to export a dataset to an excel file. I found following code on the net... ( http://www.codeproject.com/csharp/Export.asp ) Excel.ApplicationClass excel = new ApplicationClass();...
12
by: chreo | last post by:
I GIVE UP...I can't refresh datagrid (dataset) I have datagrid = dgrPaczki Datasource of that datagrid = DataSetPaczki1.PACZKI_PAKOWALNIA (forgive me Polish words) Dataadapter which fills my...
4
by: Scott | last post by:
Ok. Brand new at this so please give a little patience and help me on the baby steps. I'm rewriting something I did initially in PHP / MySql on a vb.net / mysql platform. Here's what I'm doing:...
2
by: JIM.H. | last post by:
Hello, I am quite new in asp.net, here is the code I retrieve data from a web service SVP.ThisWebService.loadMethod lm = new SVP.ThisWebService.loadMethod(); System.IO.StringReader sr = new...
1
by: ISATownsME | last post by:
HELLO world, this is my first post and heres my problem. I have an excel spreadsheet with 22 columns, and undefined amount of rows. I need vb.net to read the data from excel. Then use that data to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...

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.