473,467 Members | 1,454 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Column Type

Hi,

I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
integer and 2 & 4 are text).
Now i want to export the Table to an Excel workbook.
the next code i used is working (almost) fine.

///Code
'Create a Recordset from all the records in the Orders table.
Dim sNWind As String
Dim conn As New ADODB.Connection()
Dim rs As ADODB.Recordset
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
sNorthwind & ";")
conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
rs = conn.Execute("Orders", , ADODB.CommandTypeEnum.adCmdTable)

'Create a new workbook in Excel.
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)

'Transfer the field names to Row 1 of the worksheet:
'Note: CopyFromRecordset copies only the data and not the field
' names, so you can transfer the fieldnames by traversing the
' fields collection.
Dim n As Int32
For n = 1 To rs.Fields.Count
oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset(rs)

'Save the workbook and quit Excel.
oBook.SaveAs(sSampleFolder & "Book3.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

'Close the connection
rs.Close()
conn.Close()
///Code

There is one thing that the code does not. And that is, copying the
columnproperties(integer, text) to Excel.

Does anyone know how i can acomplish this.

Thanks in advance
Nov 21 '05 #1
14 1754
Niels,

Why are you using Adodb wherefore you use interop while there is ADONET.

Here is in my idea a complete sample how to do what you want using ADONET.

http://support.microsoft.com/default...b;EN-US;316934

I hope this helps?

Cor

"Niels" <ni****@hollandridderkerk.nl>
Hi,

I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
integer and 2 & 4 are text).
Now i want to export the Table to an Excel workbook.
the next code i used is working (almost) fine.

///Code
'Create a Recordset from all the records in the Orders table.
Dim sNWind As String
Dim conn As New ADODB.Connection()
Dim rs As ADODB.Recordset
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
sNorthwind & ";")
conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
rs = conn.Execute("Orders", , ADODB.CommandTypeEnum.adCmdTable)

'Create a new workbook in Excel.
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)

'Transfer the field names to Row 1 of the worksheet:
'Note: CopyFromRecordset copies only the data and not the field
' names, so you can transfer the fieldnames by traversing the
' fields collection.
Dim n As Int32
For n = 1 To rs.Fields.Count
oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset(rs)

'Save the workbook and quit Excel.
oBook.SaveAs(sSampleFolder & "Book3.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

'Close the connection
rs.Close()
conn.Close()
///Code

There is one thing that the code does not. And that is, copying the
columnproperties(integer, text) to Excel.

Does anyone know how i can acomplish this.

Thanks in advance

Nov 21 '05 #2
Niels,

Why are you using Adodb wherefore you use interop while there is ADONET.

Here is in my idea a complete sample how to do what you want using ADONET.

http://support.microsoft.com/default...b;EN-US;316934

I hope this helps?

Cor

"Niels" <ni****@hollandridderkerk.nl>
Hi,

I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
integer and 2 & 4 are text).
Now i want to export the Table to an Excel workbook.
the next code i used is working (almost) fine.

///Code
'Create a Recordset from all the records in the Orders table.
Dim sNWind As String
Dim conn As New ADODB.Connection()
Dim rs As ADODB.Recordset
conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
sNorthwind & ";")
conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
rs = conn.Execute("Orders", , ADODB.CommandTypeEnum.adCmdTable)

'Create a new workbook in Excel.
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
oExcel = CreateObject("Excel.Application")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)

'Transfer the field names to Row 1 of the worksheet:
'Note: CopyFromRecordset copies only the data and not the field
' names, so you can transfer the fieldnames by traversing the
' fields collection.
Dim n As Int32
For n = 1 To rs.Fields.Count
oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
Next

'Transfer the data to Excel.
oSheet.Range("A2").CopyFromRecordset(rs)

'Save the workbook and quit Excel.
oBook.SaveAs(sSampleFolder & "Book3.xls")
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()

'Close the connection
rs.Close()
conn.Close()
///Code

There is one thing that the code does not. And that is, copying the
columnproperties(integer, text) to Excel.

Does anyone know how i can acomplish this.

Thanks in advance

Nov 21 '05 #3
On Thu, 11 Nov 2004 10:23:32 +0100, "Cor Ligthert" <no************@planet.nl> wrote:

¤ Niels,
¤
¤ Why are you using Adodb wherefore you use interop while there is ADONET.
¤

CopyFromRecordset requires an ADO or DAO Recordset object. It doesn't support ADO.NET.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #4
On Thu, 11 Nov 2004 10:23:32 +0100, "Cor Ligthert" <no************@planet.nl> wrote:

¤ Niels,
¤
¤ Why are you using Adodb wherefore you use interop while there is ADONET.
¤

CopyFromRecordset requires an ADO or DAO Recordset object. It doesn't support ADO.NET.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #5
>
CopyFromRecordset requires an ADO or DAO Recordset object. It doesn't
support ADO.NET.

I know that, did I write that somewhere Paul?

Cor
Nov 21 '05 #6
>
CopyFromRecordset requires an ADO or DAO Recordset object. It doesn't
support ADO.NET.

I know that, did I write that somewhere Paul?

Cor
Nov 21 '05 #7
On 10 Nov 2004 23:23:55 -0800, ni****@hollandridderkerk.nl (Niels) wrote:

¤ Hi,
¤
¤ I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
¤ integer and 2 & 4 are text).
¤ Now i want to export the Table to an Excel workbook.
¤ the next code i used is working (almost) fine.
¤
¤ ///Code
¤ 'Create a Recordset from all the records in the Orders table.
¤ Dim sNWind As String
¤ Dim conn As New ADODB.Connection()
¤ Dim rs As ADODB.Recordset
¤ conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
¤ sNorthwind & ";")
¤ conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
¤ rs = conn.Execute("Orders", , ADODB.CommandTypeEnum.adCmdTable)
¤
¤ 'Create a new workbook in Excel.
¤ Dim oExcel As Object
¤ Dim oBook As Object
¤ Dim oSheet As Object
¤ oExcel = CreateObject("Excel.Application")
¤ oBook = oExcel.Workbooks.Add
¤ oSheet = oBook.Worksheets(1)
¤
¤ 'Transfer the field names to Row 1 of the worksheet:
¤ 'Note: CopyFromRecordset copies only the data and not the field
¤ ' names, so you can transfer the fieldnames by traversing the
¤ ' fields collection.
¤ Dim n As Int32
¤ For n = 1 To rs.Fields.Count
¤ oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
¤ Next
¤
¤ 'Transfer the data to Excel.
¤ oSheet.Range("A2").CopyFromRecordset(rs)
¤
¤ 'Save the workbook and quit Excel.
¤ oBook.SaveAs(sSampleFolder & "Book3.xls")
¤ oSheet = Nothing
¤ oBook = Nothing
¤ oExcel.Quit()
¤ oExcel = Nothing
¤ GC.Collect()
¤
¤ 'Close the connection
¤ rs.Close()
¤ conn.Close()
¤ ///Code
¤
¤ There is one thing that the code does not. And that is, copying the
¤ columnproperties(integer, text) to Excel.
¤
¤ Does anyone know how i can acomplish this.
¤

Excel determines the data types based upon the pattern of the incoming data. It does not consider
the data type of each column in the source.

I don't know how you are checking the column properties but each column should have a data type that
was determined at the time of import.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #8
On 10 Nov 2004 23:23:55 -0800, ni****@hollandridderkerk.nl (Niels) wrote:

¤ Hi,
¤
¤ I have a Acces Db with one Table. The Table has 4 rows (row 1 & 3 are
¤ integer and 2 & 4 are text).
¤ Now i want to export the Table to an Excel workbook.
¤ the next code i used is working (almost) fine.
¤
¤ ///Code
¤ 'Create a Recordset from all the records in the Orders table.
¤ Dim sNWind As String
¤ Dim conn As New ADODB.Connection()
¤ Dim rs As ADODB.Recordset
¤ conn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & _
¤ sNorthwind & ";")
¤ conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
¤ rs = conn.Execute("Orders", , ADODB.CommandTypeEnum.adCmdTable)
¤
¤ 'Create a new workbook in Excel.
¤ Dim oExcel As Object
¤ Dim oBook As Object
¤ Dim oSheet As Object
¤ oExcel = CreateObject("Excel.Application")
¤ oBook = oExcel.Workbooks.Add
¤ oSheet = oBook.Worksheets(1)
¤
¤ 'Transfer the field names to Row 1 of the worksheet:
¤ 'Note: CopyFromRecordset copies only the data and not the field
¤ ' names, so you can transfer the fieldnames by traversing the
¤ ' fields collection.
¤ Dim n As Int32
¤ For n = 1 To rs.Fields.Count
¤ oSheet.Cells(1, n).Value = rs.Fields(n - 1).Name
¤ Next
¤
¤ 'Transfer the data to Excel.
¤ oSheet.Range("A2").CopyFromRecordset(rs)
¤
¤ 'Save the workbook and quit Excel.
¤ oBook.SaveAs(sSampleFolder & "Book3.xls")
¤ oSheet = Nothing
¤ oBook = Nothing
¤ oExcel.Quit()
¤ oExcel = Nothing
¤ GC.Collect()
¤
¤ 'Close the connection
¤ rs.Close()
¤ conn.Close()
¤ ///Code
¤
¤ There is one thing that the code does not. And that is, copying the
¤ columnproperties(integer, text) to Excel.
¤
¤ Does anyone know how i can acomplish this.
¤

Excel determines the data types based upon the pattern of the incoming data. It does not consider
the data type of each column in the source.

I don't know how you are checking the column properties but each column should have a data type that
was determined at the time of import.
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #9
On Thu, 11 Nov 2004 17:08:08 +0100, "Cor Ligthert" <no************@planet.nl> wrote:

¤ >
¤ > CopyFromRecordset requires an ADO or DAO Recordset object. It doesn't
¤ > support ADO.NET.
¤ >
¤ I know that, did I write that somewhere Paul?
¤
¤ Cor
¤

You asked him why he was using ADODB when he could be using ADO.NET.

If you're suggesting he abandon both ADO *and* Excel automation then you are correct in that he can
use ADO.NET instead. Otherwise, if he needs to use Excel automation then he has no choice other than
to use ADO.

Does that make sense?
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #10
On Thu, 11 Nov 2004 17:08:08 +0100, "Cor Ligthert" <no************@planet.nl> wrote:

¤ >
¤ > CopyFromRecordset requires an ADO or DAO Recordset object. It doesn't
¤ > support ADO.NET.
¤ >
¤ I know that, did I write that somewhere Paul?
¤
¤ Cor
¤

You asked him why he was using ADODB when he could be using ADO.NET.

If you're suggesting he abandon both ADO *and* Excel automation then you are correct in that he can
use ADO.NET instead. Otherwise, if he needs to use Excel automation then he has no choice other than
to use ADO.

Does that make sense?
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #11
Thanks you all for your answers.

I have a AccesDB as input and it must be output to Excel.

I going to give the sample that Cor Ligthert gave a try.

Yesterday i tried to run my program on a machine that has no Excel
installed, and of course the program crashed.
Is it possible to make a Excel workbook without Excel being installed???

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #12
Thanks you all for your answers.

I have a AccesDB as input and it must be output to Excel.

I going to give the sample that Cor Ligthert gave a try.

Yesterday i tried to run my program on a machine that has no Excel
installed, and of course the program crashed.
Is it possible to make a Excel workbook without Excel being installed???

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #13
On Thu, 11 Nov 2004 21:40:38 -0800, Niels <Ni***@devdex.com> wrote:

¤ Thanks you all for your answers.
¤
¤ I have a AccesDB as input and it must be output to Excel.
¤
¤ I going to give the sample that Cor Ligthert gave a try.
¤
¤ Yesterday i tried to run my program on a machine that has no Excel
¤ installed, and of course the program crashed.
¤ Is it possible to make a Excel workbook without Excel being installed???

I didn't see in your code where you were creating an Excel Workbook file but there are only two
options. You either need to use Excel and automation or ADOX (Microsoft ADO Ext 2.x for DDL and
Security).

One other method would be to use a blank Excel Workbook template (file).
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #14
On Thu, 11 Nov 2004 21:40:38 -0800, Niels <Ni***@devdex.com> wrote:

¤ Thanks you all for your answers.
¤
¤ I have a AccesDB as input and it must be output to Excel.
¤
¤ I going to give the sample that Cor Ligthert gave a try.
¤
¤ Yesterday i tried to run my program on a machine that has no Excel
¤ installed, and of course the program crashed.
¤ Is it possible to make a Excel workbook without Excel being installed???

I didn't see in your code where you were creating an Excel Workbook file but there are only two
options. You either need to use Excel and automation or ADOX (Microsoft ADO Ext 2.x for DDL and
Security).

One other method would be to use a blank Excel Workbook template (file).
Paul ~~~ pc******@ameritech.net
Microsoft MVP (Visual Basic)
Nov 21 '05 #15

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

Similar topics

1
by: aspnetpal | last post by:
Hi Every1, I'm getting this error. 'Cannot specify a column width on data type smalldatetime.' I'm using SQL Server & I've check the data type in the db & it's same as smalldatetime. Any...
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
6
by: Robert Schuldenfrei | last post by:
Dear NG, After being away from C# programming for a spell, I am trying my hand at what should be a simple task. I have been hitting my head against the wall this morning. I have a simple order...
3
by: Bob Day | last post by:
VS 2003, sql How do you determine the data type of a column if its value is DBNull? 1)Table: Column1 STRING non-nullable 2) Fill to a DataSet via DataAdapter 3) dim Data_Type_Is...
4
by: =?Utf-8?B?QmFidU1hbg==?= | last post by:
Hi, I have a GridView and a SqlDataSource controls on a page. The SqlDataSource object uses stored procedures to do the CRUD operations. The DataSource has three columns one of which -...
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
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
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...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.