Groups | Blog | Home
all groups > vb.net > february 2006 >

vb.net : Ado.net to excel ?


Agnes
2/26/2006 11:45:15 PM
From
http://support.microsoft.com/default.aspx?scid=kb;EN-US;306022#XSLTH4207121122120121120120It
seems we can export the data to an excel by a simple query. However, I try
to amend that statement into "insert int [Sheet1$] select * from myInvoice
", HOwerver, it really doesn't work . Does anyone got idea ?Thanks alot
'Establish a connection to the data source.
Dim sConnectionString As String
sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sSampleFolder & _
"Book7.xls;Extended Properties=Excel 8.0;"
Dim objConn As New
System.Data.OleDb.OleDbConnection(sConnectionString)
objConn.Open()

'Add two records to the table.
Dim objCmd As New System.Data.OleDb.OleDbCommand()
objCmd.Connection = objConn
objCmd.CommandText = "Insert into [Sheet1$] (FirstName, LastName)" &
" values ('Bill', 'Brown')" <-- I try to amend
objCmd.ExecuteNonQuery()
objCmd.CommandText = "Insert into [Sheet1$] (FirstName, LastName)" &
" values ('Joe', 'Thomas')"
objCmd.ExecuteNonQuery()

'Close the connection.
objConn.Close()

Paul Clement
2/27/2006 11:12:07 AM
[quoted text, click to view]

¤ From
¤ http://support.microsoft.com/default.aspx?scid=kb;EN-US;306022#XSLTH4207121122120121120120It
¤ seems we can export the data to an excel by a simple query. However, I try
¤ to amend that statement into "insert int [Sheet1$] select * from myInvoice
¤ ", HOwerver, it really doesn't work . Does anyone got idea ?Thanks alot
¤ 'Establish a connection to the data source.
¤ Dim sConnectionString As String
¤ sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
¤ "Data Source=" & sSampleFolder & _
¤ "Book7.xls;Extended Properties=Excel 8.0;"
¤ Dim objConn As New
¤ System.Data.OleDb.OleDbConnection(sConnectionString)
¤ objConn.Open()
¤
¤ 'Add two records to the table.
¤ Dim objCmd As New System.Data.OleDb.OleDbCommand()
¤ objCmd.Connection = objConn
¤ objCmd.CommandText = "Insert into [Sheet1$] (FirstName, LastName)" &
¤ " values ('Bill', 'Brown')" <-- I try to amend
¤ objCmd.ExecuteNonQuery()
¤ objCmd.CommandText = "Insert into [Sheet1$] (FirstName, LastName)" &
¤ " values ('Joe', 'Thomas')"
¤ objCmd.ExecuteNonQuery()
¤
¤ 'Close the connection.
¤ objConn.Close()
¤

What is myInvoice? Is this an Excel Worksheet in the current Workbook opened through your
connection?


Paul
~~~~
Agnes
2/28/2006 12:00:00 AM
myInvoice is the table in SQL server,

"Paul Clement" <UseAdddressAtEndofMessage@swspectrum.com>
???????:ijc6021vi5h5k48q4u5krk60abm3r3kj0a@4ax.com...
[quoted text, click to view]

Paul Clement
2/28/2006 8:44:30 AM
[quoted text, click to view]

¤ myInvoice is the table in SQL server,
¤

You need to hook up with SQL Server as well. See if the following helps:

"INSERT INTO [Sheet1$] SELECT * FROM [ODBC;Driver={SQL
Server};Server=(local);Database=DBName;Trusted_Connection=yes].[myInvoice];"


Paul
~~~~
AddThis Social Bookmark Button